|Start of Tutorial > Start of Trail > Start of Lesson||
JTabbedPaneclass, you can have several components (usually panels) share the same space. The user chooses which component to view by selecting the tab corresponding to the desired component. If you want similar functionality without the tab interface, you might want to use a card layout instead of a tabbed pane.
To create a tabbed pane, you simply instantiate
JTabbedPane, create the components you wish it to display, and then add the components to the tabbed pane using the
Here is a picture of an application that has a tabbed pane with four tabs:
- Compile and run the application. The source file is
TabbedPaneDemo.java. You also need one image file:
See Getting Started with Swing if you need help compiling or running this application.
- Put the cursor over a tab.
After a short time, you'll see the tool tip associated with the tab. As a convenience, you can specify tool tip text when you add a component to the tabbed pane.
- Select a tab.
The tabbed pane displays the component corresponding to the tab.
TabbedPaneDemoexample shows, a tab can have a tool tip, and it can display both text and an image. The example shows the tabs in their default position, at the top of the tabbed pane. You can change the tab position to be at the left, right, or bottom of the tabbed pane using the
Below is the code from
TabbedPaneDemo.javathat creates the tabbed pane in the previous example. Note that no event-handling code is necessary. The
JTabbedPaneobject takes care of mouse events for you.ImageIcon icon = new ImageIcon("images/middle.gif"); JTabbedPane tabbedPane = new JTabbedPane(); Component panel1 = makeTextPanel("Blah"); tabbedPane.addTab("One", icon, panel1, "Does nothing"); tabbedPane.setSelectedIndex(0); Component panel2 = makeTextPanel("Blah blah"); tabbedPane.addTab("Two", icon, panel2, "Does twice as much nothing"); Component panel3 = makeTextPanel("Blah blah blah"); tabbedPane.addTab("Three", icon, panel3, "Still does nothing"); Component panel4 = makeTextPanel("Blah blah blah blah"); tabbedPane.addTab("Four", icon, panel4, "Does nothing at all");
The following tables list the commonly used
JTabbedPaneconstructors and methods. The API for using tabbed panes falls into these categories:
- Creating and Setting Up a Tabbed Pane
- Inserting, Removing, Finding, and Selecting Tabs
- Changing Tab Appearance
Creating and Setting Up a Tabbed Pane Method or Constructor Purpose
Create a tabbed pane. The first optional argument specifies where the tabs should appear. By default, the tabs appear at the top of the tabbed pane. You can specify these positions (defined in the
RIGHT. The second optional argument specifies the tab layout policy. You can specify one of these policies (defined in
SCROLL_TAB_LAYOUTScrollable tabs were introduced in 1.4 and, although supported, are not recommended. For more information, please see the Java Look and Feel Design Guidelines.
addTab(String, Icon, Component, String)
addTab(String, Icon, Component)
Add a new tab to the tabbed pane. The first argument specifies the text on the tab. The optional icon argument specifies the tab's icon. The component argument specifies the component that the tabbed pane should show when the tab is selected. The fourth argument, if present, specifies the tool tip text for the tab.
Set or get the policy that the tabbed pane uses in laying out the tabs when all the tabs do not fit within a single run. Possible values are
SCROLL_TAB_LAYOUT. The default, and preferred, policy is
WRAP_TAB_LAYOUT. Introduced in 1.4.
Set or get where the tabs appear, relative to the content. Possible values (defined in
SwingConstants, which is implemented by
Inserting, Removing, Finding, and Selecting Tabs Method Purpose
insertTab(String, Icon, Component, String, int)
Insert a tab at the specified index, where the first tab is at index 0. The arguments are the same as for
Remove the tab corresponding to the specified component or index.
Remove all tabs.
Return the index of the tab that has the specified component, title, or icon.
Select the tab that has the specified component or index. Selecting a tab has the effect of displaying its associated component.
Return the index or component for the selected tab.
Changing Tab Appearance Method Purpose
void setComponentAt(int, Component)
Set or get which component is associated with the tab at the specified index. The first tab is at index 0.
void setTitleAt(int, String)
Set or get the title of the tab at the specified index.
void setIconAt(int, Icon)
void setDisabledIconAt(int, Icon)
Set or get the icon displayed by the tab at the specified index.
void setBackgroundAt(int, Color)
void setForegroundAt(int, Color)
Set or get the background or foreground color used by the tab at the specified index. By default, a tab uses the tabbed pane's background and foreground colors. For example, if the tabbed pane's foreground is black, then each tab's title is black except for any tabs for which you specify another color using
void setEnabledAt(int, boolean)
Set or get the enabled state of the tab at the specified index.
void setMnemonicAt(int, int)
Set or get the keyboard mnemonic for accessing the specified tab. Introduced in 1.4.
This table lists examples that use
JTabbedPaneand where those examples are described.
Example Where Described Notes
This page Demonstrates a few tabbed pane features, such as tool tips and icons in tabs. The frame's size is set using
How to Use BoxLayout Uses a
JTabbedPaneas the only child of a frame's content pane. The tabs' components have different preferred sizes, and the frame uses
setSizeto set its size.
How to Use Borders Uses its tabbed pane in a manner similar to AlignmentDemo's usage.
How to Use Dialogs Has a tabbed pane in the center of a frame's content pane, with a label below it. Uses
setSize, to set the frame's size.
|Start of Tutorial > Start of Trail > Start of Lesson||
Copyright 1995-2003 Sun Microsystems, Inc. All rights reserved.