Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Create panel depending on which tab is clicked in Swing

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic