APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing

public Class JTabbedPane

extends JComponent
implements Serializable, Accessible, SwingConstants
Class Inheritance
All Implemented Interfaces
javax.swing.SwingConstants, javax.accessibility.Accessible, java.io.Serializable
Annotations
@JavaBean
defaultProperty:UI
description:A component which provides a tab folder metaphor for displaying one component from a set of components.
@SwingContainer
@SuppressWarnings:serial
Imports
java.awt.Color, .Component, .Cursor, .Dimension, .Font, .FontMetrics, .IllegalComponentStateException, .Point, .Rectangle, java.awt.event.FocusListener, .MouseEvent, java.beans.BeanProperty, .JavaBean, .Transient, java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serial, .Serializable, java.util.ArrayList, .Locale, .Objects, javax.accessibility.Accessible, .AccessibleComponent, .AccessibleContext, .AccessibleIcon, .AccessibleRole, .AccessibleSelection, .AccessibleState, .AccessibleStateSet, .AccessibleValue, javax.swing.event.ChangeEvent, .ChangeListener, javax.swing.plaf.TabbedPaneUI, .UIResource, sun.swing.SwingUtilities2

A component that lets the user switch between a group of components by clicking on a tab with a given title and/or icon. For examples and information on using tabbed panes see How to Use Tabbed Panes, a section in The Java Tutorial.

Tabs/components are added to a TabbedPane object by using the addTab and insertTab methods. A tab is represented by an index corresponding to the position it was added in, where the first tab has an index equal to 0 and the last tab has an index equal to the tab count minus 1.

The TabbedPane uses a SingleSelectionModel to represent the set of tab indices and the currently selected index. If the tab count is greater than 0, then there will always be a selected index, which by default will be initialized to the first tab. If the tab count is 0, then the selected index will be -1.

The tab title can be rendered by a Component. For example, the following produce similar results:

// In this case the look and feel renders the title for the tab.
tabbedPane.addTab("Tab", myComponent);
// In this case the custom component is responsible for rendering the
// title of the tab.
tabbedPane.addTab(null, myComponent);
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));
The latter is typically used when you want a more complex user interaction that requires custom components on the tab. For example, you could provide a custom component that animates or one that has widgets for closing the tab.

If you specify a component for a tab, the JTabbedPane will not render any text or icon you have specified for the tab.

Note

Do not use setVisible directly on a tab component to make it visible, use setSelectedComponent or setSelectedIndex methods instead.

Warning

Swing is not thread safe. For more information see Swing's Threading Policy.

Warning

Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans has been added to the java.beans package. Please see java.beans.XMLEncoder.

Authors
Dave Moore, Philip Milne, Amy Fowler
Since
1.2
See Also
SingleSelectionModel

Nested and Inner Type Summary

Modifier and TypeClass and Description
protected class
JTabbedPane.AccessibleJTabbedPane

This class implements accessibility support for the JTabbedPane class.

protected class
JTabbedPane.ModelListener

We pass ModelChanged events along to the listeners with the tabbedpane (instead of the model itself) as the event source.

private class

Field Summary

Modifier and TypeField and Description
protected transient ChangeEvent
changeEvent

Only one ChangeEvent is needed per TabPane instance since the event's only (read-only) state is the source property.

protected ChangeListener
changeListener

The changeListener is the listener we add to the model.

private boolean
protected SingleSelectionModel
model

The default selection model

private List<JTabbedPane.Page>
public static final int
SCROLL_TAB_LAYOUT

Tab layout policy for providing a subset of available tabs when all the tabs will not fit within a single run.

private int
protected int
tabPlacement

Where the tabs are placed.

private static final String
private Component
public static final int
WRAP_TAB_LAYOUT

The tab layout policy for wrapping tabs in multiple runs when all tabs will not fit within a single run.

Inherited from javax.swing.JComponent:
DEBUG_GRAPHICS_LOADEDfocusControllerivSourceComponentlistenerListpaintingChildTOOL_TIP_TEXT_KEYuiUNDEFINED_CONDITIONWHEN_ANCESTOR_OF_FOCUSED_COMPONENTWHEN_FOCUSEDWHEN_IN_FOCUSED_WINDOW

Constructor Summary

AccessConstructor and Description
public
JTabbedPane()

Creates an empty TabbedPane with a default tab placement of JTabbedPane.TOP.

public
JTabbedPane(int
the placement for the tabs relative to the content
tabPlacement
)

Creates an empty TabbedPane with the specified tab placement of either: JTabbedPane.TOP, JTabbedPane.BOTTOM, JTabbedPane.LEFT, or JTabbedPane.RIGHT.

public
JTabbedPane(int
the placement for the tabs relative to the content
tabPlacement
,
int
the policy for laying out tabs when all tabs will not fit on one run
tabLayoutPolicy
)

Creates an empty TabbedPane with the specified tab placement and tab layout policy.

Method Summary

Modifier and TypeMethod and Description
public Component

Returns:

the component
add
(Component
the component to be displayed when this tab is clicked
component
)

Overrides java.awt.Container.add.

Adds a component with a tab title defaulting to the name of the component which is the result of calling component.getName.

public Component

Returns:

the component
add
(String
the title to be displayed in this tab
title
,
Component
the component to be displayed when this tab is clicked
component
)

Overrides java.awt.Container.add.

Adds a component with the specified tab title.

public Component

Returns:

the component
add
(Component
the component to be displayed when this tab is clicked
component
,
int
the position to insert this new tab
index
)

Overrides java.awt.Container.add.

Adds a component at the specified tab index with a tab title defaulting to the name of the component.

public void
add(Component
the component to be displayed when this tab is clicked
component
,
Object
the object to be displayed in the tab
constraints
)

Overrides java.awt.Container.add.

Adds a component to the tabbed pane.

public void
add(Component
the component to be displayed when this tab is clicked
component
,
Object
the object to be displayed in the tab
constraints
,
int
the position to insert this new tab
index
)

Overrides java.awt.Container.add.

Adds a component at the specified tab index.

public void
addChangeListener(ChangeListener
the ChangeListener to add
l
)

Adds a ChangeListener to this tabbedpane.

public void
addTab(String
the title to be displayed in this tab
title
,
Icon
the icon to be displayed in this tab
icon
,
Component
the component to be displayed when this tab is clicked
component
,
String
the tooltip to be displayed for this tab
tip
)

Adds a component and tip represented by a title and/or icon, either of which can be null.

public void
addTab(String
the title to be displayed in this tab
title
,
Icon
the icon to be displayed in this tab
icon
,
Component
the component to be displayed when this tab is clicked
component
)

Adds a component represented by a title and/or icon, either of which can be null.

public void
addTab(String
the title to be displayed in this tab
title
,
Component
the component to be displayed when this tab is clicked
component
)

Adds a component represented by a title and no icon.

private void
private void
checkIndex(int index)

private static void
checkTabLayoutPolicy(int tabLayoutPolicy)

private static void
checkTabPlacement(int tabPlacement)

private void
pack-priv void
compWriteObjectNotify()

Overrides javax.swing.JComponent.compWriteObjectNotify.

This is called from Component by way of reflection.

protected ChangeListener

Returns:

a ChangeListener
createChangeListener
()

Subclasses that want to handle ChangeEvents differently can override this to return a subclass of ModelListener or another ChangeListener implementation.

protected void
fireStateChanged()

Sends a ChangeEvent, with this JTabbedPane as the source, to each registered listener.

public AccessibleContext

Returns:

an AccessibleJTabbedPane that serves as the AccessibleContext of this JTabbedPane
getAccessibleContext
()

Overrides java.awt.Component.getAccessibleContext.

Implements javax.accessibility.Accessible.getAccessibleContext.

Gets the AccessibleContext associated with this JTabbedPane.

public Color

Returns:

the Color of the tab background at index
getBackgroundAt
(int
the index of the item being queried
index
)

Returns the tab background color at index.

public Rectangle

Returns:

a Rectangle containing the tab bounds at index, or null if tab at index is not currently visible in the UI, or if there is no UI set on this tabbedpane
getBoundsAt
(int
the index to be queried
index
)

Returns the tab bounds at index.

public ChangeListener[]

Returns:

all of the ChangeListeners added or an empty array if no listeners have been added
getChangeListeners
()

Returns an array of all the ChangeListeners added to this JTabbedPane with addChangeListener.

public Component

Returns:

the Component at index
getComponentAt
(int
the index of the item being queried
index
)

Returns the component at index.

public Icon

Returns:

the icon at index
getDisabledIconAt
(int
the index of the item being queried
index
)

Returns the tab disabled icon at index.

public int

Returns:

index representing mnemonic character if one exists; otherwise returns -1
getDisplayedMnemonicIndexAt
(int
the index of the tab that the mnemonic refers to
tabIndex
)

Returns the character, as an index, that the look and feel should provide decoration for as representing the mnemonic character.

public Color

Returns:

the Color of the tab foreground at index
getForegroundAt
(int
the index of the item being queried
index
)

Returns the tab foreground color at index.

public Icon

Returns:

the icon at index
getIconAt
(int
the index of the item being queried
index
)

Returns the tab icon at index.

public int

Returns:

the key code which represents the mnemonic; -1 if a mnemonic is not specified for the tab
getMnemonicAt
(int
the index of the tab that the mnemonic refers to
tabIndex
)

Returns the keyboard mnemonic for accessing the specified tab.

public SingleSelectionModel

Returns:

the SingleSelectionModel associated with this tabbedpane
getModel
()

Returns the model associated with this tabbedpane.

public Component

Returns:

the component corresponding to the selected tab
getSelectedComponent
()

Returns the currently selected component for this tabbedpane.

public int

Returns:

the index of the selected tab
getSelectedIndex
()

Returns the currently selected index for this tabbedpane.

public Component

Returns:

the tab component at index
getTabComponentAt
(int
the index of the item being queried
index
)

Returns the tab component at index.

public int

Returns:

an integer specifying the number of tabbed pages
getTabCount
()

Returns the number of tabs in this tabbedpane.

public int

Returns:

an int specifying the policy used to layout the tabs
getTabLayoutPolicy
()

Returns the policy used by the tabbedpane to layout the tabs when all the tabs will not fit within a single run.

public int

Returns:

an int specifying the placement for the tabs
getTabPlacement
()

Returns the placement of the tabs for this tabbedpane.

public int

Returns:

an integer giving the number of rows if the tabPlacement is TOP or BOTTOM and the number of columns if tabPlacement is LEFT or RIGHT, or 0 if there is no UI set on this tabbedpane
getTabRunCount
()

Returns the number of tab runs currently used to display the tabs.

public String

Returns:

the title at index
getTitleAt
(int
the index of the item being queried
index
)

Returns the tab title at index.

public String

Returns:

the String containing the tooltip text
getToolTipText
(MouseEvent
the MouseEvent that tells where the cursor is lingering
event
)

Overrides javax.swing.JComponent.getToolTipText.

Returns the tooltip text for the component determined by the mouse event location.

public String

Returns:

a string containing the tool tip text at index
getToolTipTextAt
(int
the index of the item being queried
index
)

Returns the tab tooltip text at index.

public TabbedPaneUI

Returns:

a TabbedPaneUI object
getUI
()

Overrides javax.swing.JComponent.getUI.

Returns the UI object which implements the L&F for this component.

public String

Returns:

the string "TabbedPaneUI"
getUIClassID
()

Overrides javax.swing.JComponent.getUIClassID.

Returns the name of the UI class that implements the L&F for this component.

public int

Returns:

the tab index which intersects the location, or -1 if no tab intersects the location
indexAtLocation
(int
the x location relative to this tabbedpane
x
,
int
the y location relative to this tabbedpane
y
)

Returns the tab index corresponding to the tab whose bounds intersect the specified location.

public int

Returns:

the first tab which matches this component, or -1 if there is no tab for this component
indexOfComponent
(Component
the component for the tab
component
)

Returns the index of the tab for the specified component.

public int

Returns:

the first tab index which matches title, or -1 if no tab has this title
indexOfTab
(String
the title for the tab
title
)

Returns the first tab index with a given title, or -1 if no tab has this title.

public int

Returns:

the first tab index which matches icon, or -1 if no tab has this icon
indexOfTab
(Icon
the icon for the tab
icon
)

Returns the first tab index with a given icon, or -1 if no tab has this icon.

public int

Returns:

the first tab which matches this tab component, or -1 if there is no tab for this tab component
indexOfTabComponent
(Component
the tab component for the tab
tabComponent
)

Returns the index of the tab for the specified tab component.

public void
insertTab(String
the title to be displayed on the tab
title
,
Icon
the icon to be displayed on the tab
icon
,
Component
the component to be displayed when this tab is clicked.
component
,
String
the tooltip to be displayed for this tab
tip
,
int
the position to insert this new tab (index >= 0 && index <= getTabCount())
index
)

Inserts a new tab for the given component, at the given index, represented by the given title and/or icon, either of which may be null.

public boolean

Returns:

true if the tab at index is enabled; false otherwise
isEnabledAt
(int
the index of the item being queried
index
)

Returns whether or not the tab at index is currently enabled.

protected String

Returns:

a string representation of this JTabbedPane.
paramString
()

Overrides javax.swing.JComponent.paramString.

Returns a string representation of this JTabbedPane.

private void
readObject(ObjectInputStream s)

Hides javax.swing.JComponent.readObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

public void
remove(Component
the component to remove from the tabbedpane
component
)

Overrides java.awt.Container.remove.

Removes the specified Component from the JTabbedPane.

public void
remove(int
the index of the component to remove from the tabbedpane
index
)

Overrides java.awt.Container.remove.

Removes the tab and component which corresponds to the specified index.

public void
removeAll()

Overrides java.awt.Container.removeAll.

Removes all the tabs and their corresponding components from the tabbedpane.

public void
removeChangeListener(ChangeListener
the ChangeListener to remove
l
)

Removes a ChangeListener from this tabbedpane.

public void
removeTabAt(int
the index of the tab to be removed
index
)

Removes the tab at index.

public void
setBackgroundAt(int
the tab index where the background should be set
index
,
Color
the color to be displayed in the tab's background
background
)

Sets the background color at index to background which can be null, in which case the tab's background color will default to the background color of the tabbedpane.

public void
setComponentAt(int
the tab index where this component is being placed
index
,
Component
the component for the tab
component
)

Sets the component at index to component.

public void
setDisabledIconAt(int
the tab index where the disabled icon should be set
index
,
Icon
the icon to be displayed in the tab when disabled
disabledIcon
)

Sets the disabled icon at index to icon which can be null.

public void
setDisplayedMnemonicIndexAt(int
the index of the tab that the mnemonic refers to
tabIndex
,
int
index into the String to underline
mnemonicIndex
)

Provides a hint to the look and feel as to which character in the text should be decorated to represent the mnemonic.

public void
setEnabledAt(int
the tab index which should be enabled/disabled
index
,
boolean
whether or not the tab should be enabled
enabled
)

Sets whether or not the tab at index is enabled.

public void
setForegroundAt(int
the tab index where the foreground should be set
index
,
Color
the color to be displayed as the tab's foreground
foreground
)

Sets the foreground color at index to foreground which can be null, in which case the tab's foreground color will default to the foreground color of this tabbedpane.

public void
setIconAt(int
the tab index where the icon should be set
index
,
Icon
the icon to be displayed in the tab
icon
)

Sets the icon at index to icon which can be null.

public void
setMnemonicAt(int
the index of the tab that the mnemonic refers to
tabIndex
,
int
the key code which represents the mnemonic
mnemonic
)

Sets the keyboard mnemonic for accessing the specified tab.

public void
setModel(SingleSelectionModel
the model to be used
model
)

Sets the model to be used with this tabbedpane.

public void
setSelectedComponent(Component
the selected Component for this TabbedPane
c
)

Sets the selected component for this tabbedpane.

public void
setSelectedIndex(int
the index to be selected
index
)

Sets the selected index for this tabbedpane.

private void
setSelectedIndexImpl(int index, boolean doAccessibleChanges)

public void
setTabComponentAt(int
the tab index where the component should be set
index
,
Component
the component to render the title for the specified tab
component
)

Sets the component that is responsible for rendering the title for the specified tab.

public void
setTabLayoutPolicy(int
the policy used to layout the tabs
tabLayoutPolicy
)

Sets the policy which the tabbedpane will use in laying out the tabs when all the tabs will not fit within a single run.

public void
setTabPlacement(int
the placement for the tabs relative to the content
tabPlacement
)

Sets the tab placement for this tabbedpane.

public void
setTitleAt(int
the tab index where the title should be set
index
,
String
the title to be displayed in the tab
title
)

Sets the title at index to title which can be null.

public void
setToolTipTextAt(int
the tab index where the tooltip text should be set
index
,
String
the tooltip text to be displayed for the tab
toolTipText
)

Sets the tooltip text at index to toolTipText which can be null.

public void
setUI(TabbedPaneUI
the new UI object
ui
)

Sets the UI object which implements the L&F for this component.

public void
updateUI()

Overrides javax.swing.JComponent.updateUI.

Resets the UI property to a value from the current look and feel.

private void
writeObject(ObjectOutputStream s)

Hides javax.swing.JComponent.writeObject.

See readObject and writeObject in JComponent for more information about serialization in Swing.

Inherited from javax.swing.JComponent:
_paintImmediatelyaddAncestorListeneraddNotifyaddVetoableChangeListeneralwaysOnTopcheckIfChildObscuredBySiblingclientPropertyChangedcomponentInputMapChangedcomputeVisibleRectcomputeVisibleRectcontainscreateToolTipdisabledndDonedropLocationForPointenablefirePropertyChangefirePropertyChangefirePropertyChangefireVetoableChangegetActionForKeyStrokegetActionMapgetActionMapgetAlignmentXgetAlignmentYgetAncestorListenersgetAutoscrollsgetBaselinegetBaselineResizeBehaviorgetBordergetBoundsgetClientPropertygetComponentGraphicsgetComponentPopupMenugetConditionForKeyStrokegetCreatedDoubleBuffergetDebugGraphicsOptionsgetDefaultLocalegetFontMetricsgetGraphicsgetGraphicsInvokedgetHeightgetInheritsPopupMenugetInputMapgetInputMapgetInputMapgetInputVerifiergetInsetsgetInsetsgetListenersgetLocationgetManagingFocusBackwardTraversalKeysgetManagingFocusForwardTraversalKeysgetMaximumSizegetMinimumSizegetNextFocusableComponentgetPopupLocationgetPreferredSizegetRegisteredKeyStrokesgetRootPanegetSizegetToolTipLocationgetToolTipTextgetTopLevelAncestorgetTransferHandlergetVerifyInputWhenFocusTargetgetVetoableChangeListenersgetVisibleRectgetWidthgetWriteObjCountergetXgetYgrabFocushideisDoubleBufferedisLightweightComponentisManagingFocusisOpaqueisOptimizedDrawingEnabledisPaintingisPaintingForPrintisPaintingOriginisPaintingTileisRequestFocusEnabledisValidateRootpaintpaintBorderpaintChildrenpaintComponentpaintForceDoubleBufferedpaintImmediatelypaintImmediatelypaintToOffscreenprintprintAllprintBorderprintChildrenprintComponentprocessComponentKeyEventprocessKeyBindingprocessKeyBindingsprocessKeyBindingsForAllComponentsprocessKeyEventprocessMouseEventprocessMouseMotionEventputClientPropertyrectangleIsObscuredregisterKeyboardActionregisterKeyboardActionremoveAncestorListenerremoveNotifyremoveVetoableChangeListenerrepaintrepaintrequestDefaultFocusrequestFocusrequestFocusrequestFocusInWindowrequestFocusInWindowresetKeyboardActionsreshaperevalidatesafelyGetGraphicssafelyGetGraphicsscrollRectToVisiblesetActionMapsetAlignmentXsetAlignmentYsetAutoscrollssetBackgroundsetBordersetComponentPopupMenusetCreatedDoubleBuffersetDebugGraphicsOptionssetDefaultLocalesetDoubleBufferedsetDropLocationsetEnabledsetFocusTraversalKeyssetFontsetForegroundsetInheritsPopupMenusetInputMapsetInputVerifiersetMaximumSizesetMinimumSizesetNextFocusableComponentsetOpaquesetPaintingChildsetPreferredSizesetRequestFocusEnabledsetToolTipTextsetTransferHandlersetUIsetUIPropertysetVerifyInputWhenFocusTargetsetVisiblesetWriteObjCountershouldDebugGraphicssuperProcessMouseMotionEventunregisterKeyboardActionupdate