javax.swing

Class JTabbedPane

Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable, SwingConstants

public class JTabbedPane
extends JComponent
implements Serializable, Accessible, SwingConstants

This is a container for components where only one component is displayed at a given time and the displayed component can be switched by clicking on tabs.

Tabs can be oriented in several ways. They can be above, below, left and right of the component. Tabs can either wrap around (by creating multiple rows of tabs) or they can be scrolled (where only a subset of the tabs can be seen at once). More tabs can be added by calling the add/addTab/insertTab methods.

See Also:
Serialized Form

Nested Class Summary

protected class
JTabbedPane.AccessibleJTabbedPane
Accessibility support for JTabbedPane.
protected class
JTabbedPane.ModelListener
A helper class that listens for changes to the model.

Nested classes/interfaces inherited from class javax.swing.JComponent

JComponent.AccessibleJComponent

Nested classes/interfaces inherited from class java.awt.Container

Container.AccessibleAWTContainer

Nested classes/interfaces inherited from class java.awt.Component

Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy

Field Summary

static int
SCROLL_TAB_LAYOUT
Indicates that the TabbedPane is in scrolling mode.
static int
WRAP_TAB_LAYOUT
Indicates that the TabbedPane is in wrap mode.
protected ChangeEvent
changeEvent
The changeEvent used to fire changes to listeners.
protected ChangeListener
changeListener
The listener that listens to the model.
protected SingleSelectionModel
model
The model that describes this JTabbedPane.
protected int
tabPlacement
The current tabPlacement of the TabbedPane.

Fields inherited from class javax.swing.JComponent

TOOL_TIP_TEXT_KEY, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW, accessibleContext, listenerList, ui

Fields inherited from class java.awt.Component

BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT

Fields inherited from interface java.awt.image.ImageObserver

ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH

Fields inherited from interface javax.swing.SwingConstants

BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST

Constructor Summary

JTabbedPane()
Creates a new JTabbedPane object with tabs on top and using wrap tab layout.
JTabbedPane(int tabPlacement)
Creates a new JTabbedPane object using wrap tab layout and the given tabPlacement, where tabPlacement can be one of the following values: SwingConstants.TOP, SwingConstants.BOTTOM, SwingConstants.LEFT or SwingConstants.RIGHT.
JTabbedPane(int tabPlacement, int tabLayoutPolicy)
Creates a new JTabbedPane object with the given tabPlacement and tabLayoutPolicy.

Method Summary

Component
add(Component component)
This method adds a tab to the JTabbedPane.
Component
add(Component component, int index)
This method adds a tab to the JTabbedPane.
void
add(Component component, Object constraints)
This method adds a tab to the JTabbedPane.
void
add(Component component, Object constraints, int index)
This method adds a tab to the JTabbedPane.
Component
add(String title, Component component)
This method adds a tab to the JTabbedPane.
void
addChangeListener(ChangeListener l)
This method adds a ChangeListener to the JTabbedPane.
void
addTab(String title, Component component)
This method adds a tab to the JTabbedPane.
void
addTab(String title, Icon icon, Component component)
This method adds a tab to the JTabbedPane.
void
addTab(String title, Icon icon, Component component, String tip)
This method adds a tab to the JTabbedPane.
protected ChangeListener
createChangeListener()
This method creates a ChangeListener that is used to listen to the model for events.
protected void
fireStateChanged()
This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
AccessibleContext
getAccessibleContext()
Returns the object that provides accessibility features for this JTabbedPane component.
Color
getBackgroundAt(int index)
This method returns the background color for the tab.
Rectangle
getBoundsAt(int index)
This method returns the bounds of the tab given the index.
ChangeListener[]
getChangeListeners()
This method returns all ChangeListeners registered with the JTabbedPane.
Component
getComponentAt(int index)
This method returns the component associated with the tab.
Icon
getDisabledIconAt(int index)
This method returns the disabled icon given the index.
int
getDisplayedMnemonicIndexAt(int tabIndex)
This method returns the mnemonic index for the tab.
Color
getForegroundAt(int index)
This method returns the foreground color for the tab.
Icon
getIconAt(int index)
This method returns the active icon given the index.
int
getMnemonicAt(int tabIndex)
This method returns the mnemonic for the tab.
SingleSelectionModel
getModel()
This method returns the model used with the JTabbedPane.
Component
getSelectedComponent()
This method returns the component at the selected index.
int
getSelectedIndex()
This method returns the index of the tab that is currently selected.
int
getTabCount()
This method returns how many tabs are in the JTabbedPane.
int
getTabLayoutPolicy()
This method returns the tabLayoutPolicy.
int
getTabPlacement()
This method returns the tabPlacement.
int
getTabRunCount()
This method returns the number of runs used to paint the JTabbedPane.
String
getTitleAt(int index)
This method returns the tab title given the index.
String
getToolTipText(MouseEvent event)
This method returns the tooltip text given a mouse event.
String
getToolTipTextAt(int index)
This method returns the tooltip string for the tab.
TabbedPaneUI
getUI()
This method returns the UI used to display the JTabbedPane.
String
getUIClassID()
This method returns a string identifier that is used to determine which UI will be used with the JTabbedPane.
int
indexAtLocation(int x, int y)
This method returns a tab index given an (x,y) location.
int
indexOfComponent(Component component)
This method finds the index of a tab given the component.
int
indexOfTab(String title)
This method finds the index of a tab given the title.
int
indexOfTab(Icon icon)
This method finds the index of a tab given the icon.
void
insertTab(String title, Icon icon, Component component, String tip, int index)
This method inserts tabs into JTabbedPane.
boolean
isEnabledAt(int index)
This method returns whether this tab is enabled.
protected String
paramString()
Returns a string describing the attributes for the JTabbedPane component, for use in debugging.
void
remove(int index)
Removes the tab and component which corresponds to the specified index.
void
remove(Component component)
Removes the specified Component from the JTabbedPane.
void
removeAll()
This method removes all tabs and associated components from the JTabbedPane.
void
removeChangeListener(ChangeListener l)
This method removes a ChangeListener to the JTabbedPane.
void
removeTabAt(int index)
Removes the tab at index.
void
setBackgroundAt(int index, Color background)
This method sets the background color of the tab.
void
setComponentAt(int index, Component component)
This method sets the component associated with the tab.
void
setDisabledIconAt(int index, Icon disabledIcon)
This method sets the disabled icon of the tab.
void
setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)
This method sets the displayed mnemonic index of the tab.
void
setEnabledAt(int index, boolean enabled)
This method sets whether the tab is enabled.
void
setForegroundAt(int index, Color foreground)
This method sets the foreground color of the tab.
void
setIconAt(int index, Icon icon)
This method sets the icon of the tab.
void
setMnemonicAt(int tabIndex, int mnemonic)
This method sets the mnemonic for the tab.
void
setModel(SingleSelectionModel m)
This method changes the model property of the JTabbedPane.
void
setSelectedComponent(Component c)
This method sets the component at the selected index.
void
setSelectedIndex(int index)
This method sets the selected index.
void
setTabLayoutPolicy(int tabLayoutPolicy)
This method changes the tabLayoutPolicy property of the JTabbedPane.
void
setTabPlacement(int tabPlacement)
This method changes the tabPlacement property of the JTabbedPane.
void
setTitleAt(int index, String title)
This method sets the title of the tab.
void
setToolTipTextAt(int index, String toolTipText)
This method sets the tooltip text of the tab.
void
setUI(TabbedPaneUI ui)
This method sets the UI used to display the JTabbedPane.
void
updateUI()
This method restores the UI to the defaults given by the UIManager.

Methods inherited from class javax.swing.JComponent

addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI

Methods inherited from class java.awt.Container

add, add, add, add, add, addContainerListener, addImpl, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, extends EventListener> T[] getListeners, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponentZOrder, getComponents, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, paramString, preferredSize, print, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, removeNotify, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, setLayout, transferFocusDownCycle, update, validate, validateTree

Methods inherited from class java.awt.Component

action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, extends EventListener> T[] getListeners, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFocusable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFocusable, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

SCROLL_TAB_LAYOUT

public static final int SCROLL_TAB_LAYOUT
Indicates that the TabbedPane is in scrolling mode.
Field Value:
1

WRAP_TAB_LAYOUT

public static final int WRAP_TAB_LAYOUT
Indicates that the TabbedPane is in wrap mode.
Field Value:
0

changeEvent

protected ChangeEvent changeEvent
The changeEvent used to fire changes to listeners.

changeListener

protected ChangeListener changeListener
The listener that listens to the model.

model

protected SingleSelectionModel model
The model that describes this JTabbedPane.

tabPlacement

protected int tabPlacement
The current tabPlacement of the TabbedPane.

Constructor Details

JTabbedPane

public JTabbedPane()
Creates a new JTabbedPane object with tabs on top and using wrap tab layout.

JTabbedPane

public JTabbedPane(int tabPlacement)
Creates a new JTabbedPane object using wrap tab layout and the given tabPlacement, where tabPlacement can be one of the following values: SwingConstants.TOP, SwingConstants.BOTTOM, SwingConstants.LEFT or SwingConstants.RIGHT.
Parameters:
tabPlacement - where the tabs will be placed

JTabbedPane

public JTabbedPane(int tabPlacement,
                   int tabLayoutPolicy)
Creates a new JTabbedPane object with the given tabPlacement and tabLayoutPolicy. The tabPlacement can be one of the following values: SwingConstants.TOP, SwingConstants.BOTTOM, SwingConstants.LEFT or SwingConstants.RIGHT. The tabLayoutPolicy can be either SCROLL_TAB_LAYOUT or WRAP_TAB_LAYOUT.
Parameters:
tabPlacement - where the tabs will be placed
tabLayoutPolicy - the way tabs will be placed
Throws:
IllegalArgumentException - If tabLayoutPolicy or tabPlacement are not valid.

Method Details

add

public Component add(Component component)
This method adds a tab to the JTabbedPane. The title of the tab is the Component's name. If the Component is an instance of UIResource, it doesn't add the tab and instead add the component directly to the JTabbedPane.
Overrides:
add in interface Container
Parameters:
component - The associated component.
Returns:
The Component that was added.

add

public Component add(Component component,
                     int index)
This method adds a tab to the JTabbedPane. If the Component is an instance of UIResource, it doesn't add the tab and instead add the component directly to the JTabbedPane.
Overrides:
add in interface Container
Parameters:
component - The associated component.
index - The index to insert the tab at.
Returns:
The Component that was added.

add

public void add(Component component,
                Object constraints)
This method adds a tab to the JTabbedPane. If the Component is an instance of UIResource, it doesn't add the tab and instead add the component directly to the JTabbedPane. If the constraints object is an icon, it will be used as the tab's icon. If the constraints object is a string, we will use it as the title.
Overrides:
add in interface Container
Parameters:
component - The associated component.
constraints - The constraints object.

add

public void add(Component component,
                Object constraints,
                int index)
This method adds a tab to the JTabbedPane. If the Component is an instance of UIResource, it doesn't add the tab and instead add the component directly to the JTabbedPane. If the constraints object is an icon, it will be used as the tab's icon. If the constraints object is a string, we will use it as the title.
Overrides:
add in interface Container
Parameters:
component - The associated component.
constraints - The constraints object.
index - The index to insert the tab at.

add

public Component add(String title,
                     Component component)
This method adds a tab to the JTabbedPane. If the Component is an instance of UIResource, it doesn't add the tab and instead add the component directly to the JTabbedPane.
Overrides:
add in interface Container
Parameters:
title - the title of the tab; may be null
component - the associated component
Returns:
The Component that was added.

addChangeListener

public void addChangeListener(ChangeListener l)
This method adds a ChangeListener to the JTabbedPane.
Parameters:
l - The ChangeListener to add.

addTab

public void addTab(String title,
                   Component component)
This method adds a tab to the JTabbedPane.
Parameters:
title - the title of the tab; may be null
component - the associated component

addTab

public void addTab(String title,
                   Icon icon,
                   Component component)
This method adds a tab to the JTabbedPane.
Parameters:
title - the title of the tab; may be null
icon - the icon for the tab; may be null
component - the associated component

addTab

public void addTab(String title,
                   Icon icon,
                   Component component,
                   String tip)
This method adds a tab to the JTabbedPane.
Parameters:
title - the title of the tab; may be null
icon - the icon for the tab; may be null
component - the associated component
tip - the associated tooltip

createChangeListener

protected ChangeListener createChangeListener()
This method creates a ChangeListener that is used to listen to the model for events.
Returns:
A ChangeListener to listen to the model.

fireStateChanged

protected void fireStateChanged()
This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
</