A component that lets the user switch between a group of components by clicking on a tab with a given title and/or icon. For examples and information on using tabbed panes see
How to Use Tabbed Panes , a section in
The Java Tutorial .
Tabs/components are added to a TabbedPane
object by using the addTab
and insertTab
methods. A tab is represented by an index corresponding to the position it was added in, where the first tab has an index equal to 0 and the last tab has an index equal to the tab count minus 1.
The TabbedPane
uses a SingleSelectionModel
to represent the set of tab indices and the currently selected index. If the tab count is greater than 0, then there will always be a selected index, which by default will be initialized to the first tab. If the tab count is 0, then the selected index will be -1.
The tab title can be rendered by a Component
. For example, the following produce similar results:
// In this case the look and feel renders the title for the tab.
tabbedPane.addTab("Tab", myComponent);
// In this case the custom component is responsible for rendering the
// title of the tab.
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));
The latter is typically used when you want a more complex user interaction that requires custom components on the tab. For example, you could provide a custom component that animates or one that has widgets for closing the tab.
If you specify a component for a tab, the JTabbedPane
will not render any text or icon you have specified for the tab.
Note: Do not use setVisible
directly on a tab component to make it visible, use setSelectedComponent
or setSelectedIndex
methods instead.
Warning: Swing is not thread safe. For more information see Swing's Threading Policy .
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans
package. Please see XMLEncoder
.