Java: JTabbedPane

Description. The JTabbedPane container allows many panels to occupy the same area of the interface, and the user may select which to show by clicking on a tab. A tab may also be selected by the program.


   JTabbedPane tp = new JTabbedPane();   // Defaults to tabs along the top edge.
   JTabbedPane tp = new JTabbedPane(edge);

Where edge specifies which edge the tabs are on

Adding tabs to the JTabbedPane

Add tabs to a tabbed pane by calling addTab and passing it a String title and a component (eg a JPanel) to display in that tab. For example,

JTabbedPane display = new JTabbedPane();
display.addTab("Diagram View", diagramPanel);
display.addTab("SQL View"    , sqlPanel);
display.addTab("Instructions", instructionPanel);

Selecting the tab to display

A tab can be selected by the program using setSelectedIndex().

display.setSelectedIndex(1);  // Display SQL View tab

Listening for a tab change

When the user selects a tab, a ChangeEvent is fired. Add a ChangeListener to receive notification of the tab change.

   ______.addChangeListener(ChangeListener cl);

Here's more about using a ChangeListener to detect tab changes: Determining When the Selected Tab Changes in a JTabbedPane Container