Java programming course: 14.15 JTabbedPane


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


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Friday, 19 June 2026

Captcha Image