Skip to main content
deleted 19 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Java Swing best practice 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 sub-tabs to main panes
createPane.addTab("Create Entity",null, createEntityTab);
createPane.addTab("Create Deal", null, createDealTab);

customizePane.addTab("Customize Entity", null, customizeEntityTab);
customizePane.addTab("Customize Deal", null, customizeDealTab);

viewPane.addTab("View Entity", null, viewEntityTab);
viewPane.addTab("View Deal", null, viewDealTab);

//add main panes to main tabs
createTab.add(createPane);
customizeTab.add(customizePane);
viewTab.add(viewPane);

//add main tabs to mane pane
mainPane.addTab("Create", null, createTab, "");
mainPane.addTab("Customize", null, customizeTab, "");
mainPane.addTab("View", null, viewTab, "");

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 CreateEntityCreateEntity 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?

**II know using nullnull in layout is bad practice. It's just for the question Thanks in advance!.

Java Swing best practice

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 sub-tabs to main panes
createPane.addTab("Create Entity",null, createEntityTab);
createPane.addTab("Create Deal", null, createDealTab);

customizePane.addTab("Customize Entity", null, customizeEntityTab);
customizePane.addTab("Customize Deal", null, customizeDealTab);

viewPane.addTab("View Entity", null, viewEntityTab);
viewPane.addTab("View Deal", null, viewDealTab);

//add main panes to main tabs
createTab.add(createPane);
customizeTab.add(customizePane);
viewTab.add(viewPane);

//add main tabs to mane pane
mainPane.addTab("Create", null, createTab, "");
mainPane.addTab("Customize", null, customizeTab, "");
mainPane.addTab("View", null, viewTab, "");

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 Thanks in advance!

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 sub-tabs to main panes
createPane.addTab("Create Entity",null, createEntityTab);
createPane.addTab("Create Deal", null, createDealTab);

customizePane.addTab("Customize Entity", null, customizeEntityTab);
customizePane.addTab("Customize Deal", null, customizeDealTab);

viewPane.addTab("View Entity", null, viewEntityTab);
viewPane.addTab("View Deal", null, viewDealTab);

//add main panes to main tabs
createTab.add(createPane);
customizeTab.add(customizePane);
viewTab.add(viewPane);

//add main tabs to mane pane
mainPane.addTab("Create", null, createTab, "");
mainPane.addTab("Customize", null, customizeTab, "");
mainPane.addTab("View", null, viewTab, "");

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.

Source Link
Jona
  • 21
  • 3

Java Swing best practice

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 sub-tabs to main panes
createPane.addTab("Create Entity",null, createEntityTab);
createPane.addTab("Create Deal", null, createDealTab);

customizePane.addTab("Customize Entity", null, customizeEntityTab);
customizePane.addTab("Customize Deal", null, customizeDealTab);

viewPane.addTab("View Entity", null, viewEntityTab);
viewPane.addTab("View Deal", null, viewDealTab);

//add main panes to main tabs
createTab.add(createPane);
customizeTab.add(customizePane);
viewTab.add(viewPane);

//add main tabs to mane pane
mainPane.addTab("Create", null, createTab, "");
mainPane.addTab("Customize", null, customizeTab, "");
mainPane.addTab("View", null, viewTab, "");

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 Thanks in advance!