public class TabbedContainer extends JComponent implements Accessible
The first difference from a JTabbedPane is that it is entirely model driven - the tab contents are in a data model owned by the displayer. It is not strictly necessary for the contained components to even be installed in the AWT hierarchy when not displayed.
Other differences are more flexibility in the way tabs are displayed by completely separating the implementation and UI for that from that of displaying the contents.
Other interesting aspects are the ability of TabDataModel to deliver complex, granular events in a single pass with no information loss. Generally, great effort has been gone to to conflate nothing - that is, adding a component does not equal selecting it does not equal changing focus, et. cetera, leaving these decisions more in the hands of the user of the control.
It is possible to implement a subclass which provides the API of JTabbedPane, making it a drop-in replacement.
There are several UI styles a TabbedContainer
can have. The type
is passed as an argument to the constructor (support for changing these on the
fly may be added in the future, but such a change is a very heavyweight operation,
and is only desirable to enable use of this component in its various permutations
inside GUI designers). The following styles are supported:
Customizing the appearance of tabs
Tabs are customized by providing a different UI delegate for the tab displayer component,
via UIManager, in the same manner as any standard Swing component; for TYPE_SLIDING
tabs, simply implementing an alternate UI delegate for the buttons used to represent tabs
is all that is needed.
Managing user events on tabs
When a user clicks a tab, the TabbedContainer will fire an action event to all of its listeners.
This action event will always be an instance of TabActionEvent
, which can provide
the index of the tab that was pressed, and the command name of the action that was performed.
A client which wants to handle the event itself (for example, the asking a user if they want
to save data, and possibly vetoing the closing of a tab) may veto (or take full responsibility
for performing) the action by consuming the TabActionEvent.
Indication of focus and the "activated" state
The property active
is provided to allow a tabbed container to indicate that it
contains the currently focused component. However, no effort is made to track focus on the
part of the tabbed control - there is too much variability possible (for example, if
a component inside a tab opens a modal dialog, is the tab active or not?). In fact, using
keyboard focus at all to manage the activated state of the component turns out to be a potent
source of hard-to-fix, hard-to-reproduce bugs (especially when components are being added
and removed, or hidden and shown or components which do not reliably produce focus events).
What NetBeans does to solve the problem in a reliable way is the following:
TabDisplayer
,
Serialized FormJComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static String |
COMMAND_CLOSE
Action command indicating that the action event signifies the user
clicking the Close button on a tab.
|
static String |
COMMAND_CLOSE_ALL |
static String |
COMMAND_CLOSE_ALL_BUT_THIS |
static String |
COMMAND_CLOSE_GROUP |
static String |
COMMAND_DISABLE_AUTO_HIDE |
static String |
COMMAND_ENABLE_AUTO_HIDE |
static String |
COMMAND_MAXIMIZE
Command indicating a maximize (double-click) request
|
static String |
COMMAND_MINIMIZE_GROUP |
static String |
COMMAND_POPUP_REQUEST
Action command indicating that a popup menu should be shown
|
static String |
COMMAND_RESTORE_GROUP |
static String |
COMMAND_SELECT
Action command indicating that the action event fired signifies the user
selecting a tab
|
static String |
COMMAND_TOGGLE_TRANSPARENCY |
static int |
CONTENT_POLICY_ADD_ALL
Content policy in which all components contained in the data model should immediately
be added to the AWT hierarchy at the time they appear in the data model.
|
static int |
CONTENT_POLICY_ADD_ON_FIRST_USE
Content policy by which components contained in the data model are added to the AWT
hierarchy the first time they are shown, and remain their thereafter unless removed
from the data model.
|
static int |
CONTENT_POLICY_ADD_ONLY_SELECTED
Content policy by which components contained in the data model are added to the AWT
hierarchy the when they are shown, and removed immediately when the user changes tabs.
|
protected static int |
DEFAULT_CONTENT_POLICY
The default content policy, currently CONTENT_POLICY_ADD_ALL.
|
static String |
PROP_ACTIVE
Property fired when
setActive() is called |
static String |
PROP_MANAGE_TAB_POSITION
Client property applicable only to TYPE_SLIDING tabs.
|
static String |
TABBED_CONTAINER_UI_CLASS_ID
UIManager key for the UI Delegate to be used by tabbed containers.
|
static int |
TYPE_EDITOR
Creates a "editor" style displayer; typically this uses a
scrolling tabs UI for the tab displayer.
|
static int |
TYPE_SLIDING
Creates a "sliding" view, typically with tabs rendered as
buttons along the left, bottom or right edge, with no scrolling behavior for tabs.
|
static int |
TYPE_TOOLBAR
Creates a Toolbar-style displayer (the style used by the NetBeans Form Editor's
Component Inspector and a few other places in NetBeans).
|
static int |
TYPE_VIEW
Creates a "view" style displayer; typically this will have a
fixed width and a single row of tabs which get smaller as more tabs are
added, as seen in NetBeans’ Explorer window.
|
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
TabbedContainer()
Create a new pane with the default model and tabs displayer
|
TabbedContainer(int type) |
TabbedContainer(TabDataModel model)
Create a new pane with asociated model and the default tabs displayer
|
TabbedContainer(TabDataModel model,
int type)
Create a new pane with the specified model and displayer type
|
TabbedContainer(TabDataModel model,
int type,
LocationInformer locationInformer)
Deprecated.
|
TabbedContainer(TabDataModel model,
int type,
WinsysInfoForTabbed winsysInfo)
Deprecated.
|
TabbedContainer(TabDataModel model,
int type,
WinsysInfoForTabbedContainer winsysInfo)
Create a new pane with the specified model, displayer type and extra
information from winsys
|
Modifier and Type | Method and Description |
---|---|
void |
addActionListener(ActionListener listener)
Register an ActionListener.
|
void |
addNotify() |
void |
cancelRequestAttention(int tab) |
void |
cancelRequestAttention(TabData data) |
Image |
createImageOfTab(int idx)
Create an image of a single tab, suitable for use in drag and drop operations
|
int |
dropIndexOfPoint(Point location)
The index at which a tab should be inserted if a drop operation
occurs at this point.
|
AccessibleContext |
getAccessibleContext() |
ComponentConverter |
getComponentConverter()
Get the component converter which is used to fetch a component
corresponding to an element in the data model.
|
WinsysInfoForTabbedContainer |
getContainerWinsysInfo() |
int |
getContentPolicy()
Determine the policy by which components are added to the container.
|
Shape |
getDropIndication(Object dragged,
Point location)
Get a shape appropriate for drawing on the window's glass pane to indicate
where a component should appear in the tab order if it is dropped here.
|
LocationInformer |
getLocationInformer()
Deprecated.
|
TabDataModel |
getModel()
Get the data model that represents the tabs this component has.
|
SingleSelectionModel |
getSelectionModel()
Get the selection model.
|
int |
getTabCount()
Get the number of tabs.
|
Rectangle |
getTabRect(int index,
Rectangle r)
Fetch the rectangle of the tab for a given index, in the coordinate space
of this component, by reconfiguring the passed rectangle object
|
int |
getType()
Get the type of this displayer - it is either TYPE_EDITOR or TYPE_VIEW.
|
TabbedContainerUI |
getUI()
Get the ui delegate for this component
|
String |
getUIClassID()
Returns
TabbedContainer.TABBED_CONTAINER_UI_CLASS_ID |
WinsysInfoForTabbed |
getWinsysInfo()
Deprecated.
|
int |
indexOf(Component comp)
Get the index of a component
|
boolean |
isActive()
Determine if this component thinks it is "active", which
affects how the tabs are painted - typically used to indicate that
keyboard focus is somewhere within the component
|
boolean |
isPaintingOrigin() |
boolean |
isShowCloseButton()
Determine whether or not close buttons are being shown.
|
boolean |
isTransparent() |
boolean |
isValidateRoot() |
void |
paint(Graphics g) |
protected void |
postActionEvent(TabActionEvent event)
Used by the UI to post action events for selection and close operations.
|
void |
removeActionListener(ActionListener listener)
Remove an action listener.
|
void |
removeNotify() |
void |
requestAttention(int tab)
Cause the tab at the specified index to blink or otherwise suggest that
the user should click it.
|
boolean |
requestAttention(TabData data)
Cause the specified tab to blink or otherwisse suggest that the user should
click it.
|
void |
setActive(boolean active)
Set the "active" state of this tab control - this affects the
way the tabs are displayed, to indicate focus.
|
void |
setAttentionHighlight(int tab,
boolean highlight)
Turn tab highlight on/off
|
void |
setAttentionHighlight(TabData data,
boolean highlight) |
void |
setComponentConverter(ComponentConverter cc)
Set the converter that converts user objects in the data model into
components to display.
|
void |
setContentPolicy(int i)
Experimental property - alter the policy by which the components in
the model are added to the container.
|
void |
setIconAt(int index,
Icon icon) |
void |
setShowCloseButton(boolean val)
Set whether or not close buttons should be shown.
|
void |
setTitleAt(int index,
String title) |
void |
setToolTipTextAt(int index,
String toolTip) |
void |
setTransparent(boolean transparent)
Turn container transparency on/off
|
int |
tabForCoordinate(Point p)
Gets the index of the tab at point p, or -1 if no tab is there
|
void |
updateUI()
Overridden as follows: When called by the superclass constructor (before
the
type field is set), it will simply return; the
TabbedContainer constructor will call updateUI() explicitly later. |
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final String TABBED_CONTAINER_UI_CLASS_ID
public static final int TYPE_VIEW
public static final int TYPE_EDITOR
public static final int TYPE_SLIDING
This is the least scalable of the available UI types, and is intended primarily for use with a small, fixed set of tabs. By default, the position of the tab displayer will be determined based on the proximity of the container to the edges of its parent window. This can be turned off by setting the client property PROP_MANAGE_TAB_POSITION to Boolean.FALSE.
public static final int TYPE_TOOLBAR
public static final String PROP_ACTIVE
setActive()
is calledpublic static final String PROP_MANAGE_TAB_POSITION
public static final String COMMAND_CLOSE
public static final String COMMAND_SELECT
public static final String COMMAND_POPUP_REQUEST
public static final String COMMAND_MAXIMIZE
public static final String COMMAND_CLOSE_ALL
public static final String COMMAND_CLOSE_ALL_BUT_THIS
public static final String COMMAND_ENABLE_AUTO_HIDE
public static final String COMMAND_DISABLE_AUTO_HIDE
public static final String COMMAND_TOGGLE_TRANSPARENCY
public static final String COMMAND_MINIMIZE_GROUP
public static final String COMMAND_RESTORE_GROUP
public static final String COMMAND_CLOSE_GROUP
public static final int CONTENT_POLICY_ADD_ALL
public static final int CONTENT_POLICY_ADD_ON_FIRST_USE
public static final int CONTENT_POLICY_ADD_ONLY_SELECTED
protected static int DEFAULT_CONTENT_POLICY
Error
). Do not manipulate this value at runtime, it will likely become
a final field in a future release. It is a protected field only to ensure its inclusion in documentation.TabbedContainer.setContentPolicy(int)
public TabbedContainer()
public TabbedContainer(TabDataModel model)
public TabbedContainer(int type)
public TabbedContainer(TabDataModel model, int type)
model
- The model@Deprecated public TabbedContainer(TabDataModel model, int type, LocationInformer locationInformer)
@Deprecated public TabbedContainer(TabDataModel model, int type, WinsysInfoForTabbed winsysInfo)
public TabbedContainer(TabDataModel model, int type, WinsysInfoForTabbedContainer winsysInfo)
public void updateUI()
type
field is set), it will simply return; the
TabbedContainer constructor will call updateUI() explicitly later.
Will first search UIManager for a matching UI class. If non-null (by default it is set in the core/swing/plaf library), it will compare the found class name with the current UI. If they are a match, it will call TabbedContainerUI.shouldReplaceUI() to decide whether to actually do anything or not (in most cases it would just replace an instance of DefaultTabbedContainerUI with another one; but this call allows DefaultTabbedContainerUI.uichange() to update the tab displayer as needed).
If no UIManager UI class is defined, this method will silently use an instance of DefaultTabbedContainerUI.
updateUI
in class JComponent
public final int getType()
public String getUIClassID()
TabbedContainer.TABBED_CONTAINER_UI_CLASS_ID
getUIClassID
in class JComponent
public TabbedContainerUI getUI()
getUI
in class JComponent
public final void setComponentConverter(ComponentConverter cc)
For use cases where a single component is to be displayed for more than one tab, just reconfigured when the selection changes, simply supply a ComponentConverter.Fixed with the component that should be used for all tabs.
public final ComponentConverter getComponentConverter()
public final void setContentPolicy(int i)
public int getContentPolicy()
public boolean isValidateRoot()
isValidateRoot
in class JComponent
public boolean isPaintingOrigin()
isPaintingOrigin
in class JComponent
public void setToolTipTextAt(int index, String toolTip)
public final TabDataModel getModel()
public final SingleSelectionModel getSelectionModel()
public final Rectangle getTabRect(int index, Rectangle r)
public int tabForCoordinate(Point p)
public final void setActive(boolean active)
NetBeans uses an AWTEventListener to track mouse clicks, and allows components to become activated only via a mouse click or via a keyboard action or menu item which activates the component. This approach is far more robust and is the recommended usage pattern.
public final void requestAttention(int tab)
public final void cancelRequestAttention(int tab)
public final void setAttentionHighlight(int tab, boolean highlight)
tab
- public final boolean requestAttention(TabData data)
public final void cancelRequestAttention(TabData data)
public final void setAttentionHighlight(TabData data, boolean highlight)
data
- public final boolean isActive()
public final void addActionListener(ActionListener listener)
listener
- The listener to register.public final void removeActionListener(ActionListener listener)
listener
- The listener to remove.protected final void postActionEvent(TabActionEvent event)
event
- The event to be firedpublic void setIconAt(int index, Icon icon)
public void setTitleAt(int index, String title)
public Image createImageOfTab(int idx)
public int getTabCount()
getModel().size()
public final void setShowCloseButton(boolean val)
nb.tabs.suppressCloseButton
; if the system
property is not set, the default is true.public final boolean isShowCloseButton()
public int indexOf(Component comp)
public int dropIndexOfPoint(Point location)
location
- A point anywhere on the TabbedContainerpublic Shape getDropIndication(Object dragged, Point location)
dragged
- An object being dragged, or null. The object may be an instance
of TabData
or Component
, in which case a check
will be done of whether the dragged object is already in the data model,
so that attempts to drop the object over the place it already is in the
model will always return the exact indication of that tab's position.location
- A point@Deprecated public LocationInformer getLocationInformer()
@Deprecated public WinsysInfoForTabbed getWinsysInfo()
public WinsysInfoForTabbedContainer getContainerWinsysInfo()
public AccessibleContext getAccessibleContext()
getAccessibleContext
in interface Accessible
getAccessibleContext
in class Component
public boolean isTransparent()
public void setTransparent(boolean transparent)
transparent
- True to make the container transparentpublic void addNotify()
addNotify
in class JComponent
public void removeNotify()
removeNotify
in class JComponent
public void paint(Graphics g)
paint
in class JComponent