JTabbedPane

JTabbedPane is a container that allows the programmer to overlay many dialog panels on top of each other, and, allows the user to switch between these panels by clicking on a tab that is identified with a title and/or icon.

Tabs/components are added to a JTabbedPane by using the addTab() and insertTab() methods. A tab is represented by an index corresponding to its position in the list of tabs, where the first tab has an index of 0 and the last tab has an index equal to the tab count minus 1.

In the example, each createXxx() method returns a JPanel component that is used as the basis for a new tab. Each of the createXxx() methods is a static member function of a class that contains its own main() method. Notice that these main() methods do not "clash" with the main() defined in the JTabbedPaneDemo class.

The example also incorporates the "look and feel" customization code that was demonstrated at the beginning of this section.

JTabbedPane generates ChangeEvents. About the only thing you can do with a ChangeEvent object is call getSource(). Fortunately, the JTabbedPane object returned from getSource() has a sufficiently interesting interface. To output the name of the current tab selection:

[Reference: Topley, pp566-572]