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 JPanel objects are created, panelOne and panelTwo, each containing a JLabel
  • A JTabbedPane is created to serve as the container for some tabs, and then the addTab() 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 of JPanel

This should result in the following being displayed:

JTabbedPane

If you click on Tab 2 the display will switch to the other panel:

JTabbedPane with second tab active