The JTabbedPane Class
If you’ve ever right-clicked on the desktop to set your
Display Properties in Windows, you already know what a JTabbedPane is. It’s a container with labeled
tabs (e.g., Themes, Screen Saver, Appearance). When you click on a tab, a
new set of controls is shown in the body of the JTabbedPane. In Swing, JTabbedPane is simply a specialized
container.
Each tab has a name. To add a tab to the JTabbedPane, simply call addTab(). You’ll need to
specify the name of the tab as well as a component that supplies the tab’s
contents. Typically, it’s a container holding other components.
Even though the JTabbedPane only
shows one set of components at a time, be aware that all the components on
all the pages are alive and in memory at one time. If you have components
that hog processor time or memory, try to put them into a “sleep” state
when they are not showing.
The following example shows how to create a JTabbedPane. It adds standard Swing components
to a tab named Controls. The second tab is filled with a scrollable image,
which was presented in the previous examples.
//file: TabbedPaneFrame.javaimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.border.*;publicclassTabbedPaneFrame{publicstaticvoidmain(String[]args){JFrameframe=newJFrame("TabbedPaneFrame");JTabbedPanetabby=newJTabbedPane();// create the controls paneJPanelcontrols=newJPanel();controls.add(newJLabel("Service:"));JListlist=newJList(newString ...