Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Create panel depending on which tab is clicked in Swing

 
Raymond Poon
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on which tab is clicked in the JTabbedPane, a corresponding panel will be created.
I have created a simple test program as follows.


Currently all the source code are in the same file.
I like to extract all the panel codes and put them in their own files like the following files.
How to do that?
Is this the correct approach or something should be done in ChangeListener?
Panel1.java



JTabbedPane_Panel.java


 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you move the code that creates your panels from one file to their own, that suggests you will be creating new classes for each panel. So, to add those panels to your JTabbedPane, you would do something like:

tabbedPane.addTab("Tab 1", icon, new PanelOnePane( possible arguments ),
"Does nothing");

where PanelOnePane( possible arguments ) is a panel building class that you separated from your current single file that would extend JPanel.

Of course, you could instantiate the PanelOnePane before adding it to your tabbedPane with

PanelOnePane panelOnePane = new PanelOnePane( possible arguments );

Make sense?
 
Paper has not yet been outlawed – so let your Java Swing application print with wild abandon: JPDF
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!