JTabbedPane
The JTabbedPane class displays separate titled "tabs", each of which allows the user to switch between by clicking on a tab:
JPanel panelOne = new JPanel();
panelOne.add(new JLabel("This is the first panel"));
JPanel panelTwo = new JPanel();
panelTwo.add(new JLabel("This is the second panel"));
JTabbedPane tabPane = new JTabbedPane();
tabPane.addTab("Tab 1", panelOne);
tabPane.addTab("Tab 2", panelTwo);
panel.add(tabPane);
- Two
JPanelobjects are created,panelOneandpanelTwo, each containing aJLabel - A
JTabbedPaneis created to serve as the container for some tabs, and then theaddTab()method is invoked upon it to add the panels. The first method argument is the text that should appear in the tab and the second argument is any component, although you would in most cases add instances ofJPanel
This should result in the following being displayed:
If you click on Tab 2 the display will switch to the other panel: