Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up

How to Use Tabbed Panes

With the JTabbedPane(in the API reference documentation) class, you can have several components (usually JPanel objects) share the same space. The user can choose 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 the CardLayout(in the Creating a User Interface trail) layout manager instead of JTabbedPane.

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 [PENDING] method.

Here is a picture of an application that uses three tabbed panes:

Try this:
  1. Compile and run the application. The source file is
    See Getting Started with Swing if you need help.
  2. 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.
  3. Select a tab.
    The tabbed pane displays the component corresponding to the tab.

As the TabbedPaneDemo example shows [actually, it doesn't yet], a tabbed pane can display both text and an image.

Below is the code from that creates the tabbed pane in the previous example. Note that no event-handling code is necessary. The JTabbedPane object takes care of handling user input for you.

JTabbedPane tabbedPane = new JTabbedPane();

Component panel1 = makeTextPanel("Blah");
tabbedPane.addTab("One", null, panel1, "Does nothing");
Component panel2 = makeTextPanel("Blah blah");
tabbedPane.addTab("Two", null, panel2, "Does nothing");
Component panel3 = makeTextPanel("Blah blah blah");
tabbedPane.addTab("Three", null, panel3, "Does nothing");
Component panel4 = makeTextPanel("Blah blah blah blah");
tabbedPane.addTab("Four", null, panel4, "Does nothing");
. . .
The following tables list the commonly used JTabbedPane methods and constructors.

[PENDING: add API tables]

Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up