Using the JFC/Swing Packages |
With theJTabbedPane
class, you can have several components (usuallyJPanel
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 theCardLayout
layout manager instead ofJTabbedPane
.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 theaddTab
method.Here is a picture of an application that uses three tabbed panes:
Try this:
- Compile and run the application. The source file is
TabbedPaneDemo.java
.
See Getting Started with Swing if you need help.- 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.
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
TabbedPaneDemo.java
that creates the tabbed pane in the previous example. Note that no event-handling code is necessary. TheJTabbedPane
object takes care of handling user input for you.The following tables list the commonly usedJTabbedPane tabbedPane = new JTabbedPane(); Component panel1 = makeTextPanel("Blah"); tabbedPane.addTab("One", null, panel1, "Does nothing"); tabbedPane.setSelectedIndex(0); 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"); . . . add(tabbedPane);JTabbedPane
methods and constructors.[PENDING: add API tables]
Using the JFC/Swing Packages |