# Simple GUI using tabs to separate application windows

I'm designing a simple GUI using tabs to separate each 'window' of the application. There are 3 main tabs, each with 2 sub-tabs.

In one class I create the entire hierarchy of panes and panels:

mainPane = new JTabbedPane();
mainPane.setBounds(0, 0, WIDTH, HEIGHT);

//instantiate main tabs, get panes added to them
createTab = new JPanel();
createTab.setLayout(null);

customizeTab = new JPanel();
customizeTab.setLayout(null);

viewTab = new JPanel();
viewTab.setLayout(null);

//instantiate main panes, get sub-tabs added to them
createPane = new JTabbedPane();
createPane.setBounds(0, 0, WIDTH, HEIGHT);

customizePane = new JTabbedPane();
customizePane.setBounds(0, 0, WIDTH, HEIGHT);

viewPane = new JTabbedPane();
viewPane.setBounds(0, 0, WIDTH, HEIGHT);

//instantiate sub-tabs, get elements added to them
createEntityTab = new JPanel();
createEntityTab.setLayout(null);

createDealTab = new JPanel();
createDealTab.setLayout(null);

customizeEntityTab = new JPanel();
customizeEntityTab.setLayout(null);

customizeDealTab = new JPanel();
customizeDealTab.setLayout(null);

viewEntityTab = new JPanel();
viewEntityTab.setLayout(null);

viewDealTab = new JPanel();
viewDealTab.setLayout(null);

//add main panes to main tabs

//add main tabs to mane pane


and now to add elements to each sub-tab, I would like to do something like this:

new CreateEntity(createEntityTab);
new CreateDeal(createDealTab);


i.e., passing the "sub-tab" and then adding elements in the CreateEntity class.

This seems to work, but my question is the following:

Is using a constructor to pass a JPanel, and then adding elements in the new class considered bad coding?

I know using null in layout is bad practice. It's just for the question.

• Your question would be clearer if you could provide the entire CreateEntity class. – Simon Forsberg May 25 '14 at 15:03
• I have down-voted this question because it is not asking for a code review, but it is trying to debate what a best-practice would be. Discussing best-practice is not on-scope for Code Review. There is some code to review, so closing this question is not appropriate though. – rolfl May 25 '14 at 15:29

Is using a constructor to pass a JPanel, and then adding elements in the new class considered bad coding?

Not necessarily. However, if you only create an instance of the other class just to do things in the constructor, then you don't need the new class - and in that case it is considered bad coding.

If that's all you do in the constructor, then move the code from the constructor to a method (which you can place more or less anywhere, but I'd recommend having it in the original class unless you feel that the original class gets too big).

private void createEntityTab(JPanel panel) {
...
...
}


Actually, it might be even better to let this method return the JPanel, so that you can use this code:

createPane.addTab("Create Entity", null, createEntityTab());


And use the method:

private JPanel createEntityTab() {
JPanel panel = new JPanel();
panel.setLayout(null);
...

• Welcome to Code Review! I think that technically your answer is correct, but I don't think that is what the OP is doing here. We have understood the question in two different ways, and I think that for each way, both of us is right. As I understand, the CreateEntity class extends Object and only contains a constructor (no fields, no methods, no nothing!) – Simon Forsberg May 25 '14 at 15:07