Ask Questions?

View Latest Questions



Posted on: July 24, 2006 at 12:00 AM
The JTabbedPane container allows many panels.

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();   // Tabs along the top edge.
   JTabbedPane tp = new JTabbedPane(edge);

Where edge specifies which edge the tabs are on

  • JTabbedPane.TOP (default)
  • JTabbedPane.RIGHT
  • JTabbedPane.BOTTOM
  • JTabbedPane.LEFT

Adding tabs to the JTabbedPane

Add tabs to a tabbed pane by calling addTab and passing it a String title and a component (typically 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);
Copyleft 2005 Fred Swartz MIT License