javax.swing
public class JLayeredPane extends JComponent implements Accessible
Container
semantics.
Each child component of a Layered Pane
is placed within one
of several layers. JLayeredPane
defines a set of standard
layers. The pre-defined sets are (in the order from button to top):
DEFAULT_LAYER
PALETTE_LAYER
MODAL_LAYER
POPUP_LAYER
DRAG_LAYER
A child is in exactly one of these layers at any time, though there may be other layers if someone creates them.
You can add a component to a specific layer using the
Container.add(Component, Object)
method. I.e.
layeredPane.add(comp, JLayeredPane.MODAL_LAYER)
will add the
component comp
to the modal layer of layeredPane
.
To change the layer of a component that is already a child of
a JLayeredPane
, use the setLayer(Component, int)
method.
The purpose of this class is to translate this view of "layers" into a
contiguous array of components: the one held in our ancestor,
Container
.
There is a precise set of words we will use to refer to numbers within this class:
component
array held in our ancestor,
Container
, from [0 .. component.length)
. The drawing
rule with indices is that 0 is drawn last.int
specifying a layer within this component. Negative
numbers are drawn first, then layer 0, then positive numbered layers, in
ascending order.Note: the layer numbering order is the reverse of the component indexing and position order
Modifier and Type | Class and Description |
---|---|
protected class |
JLayeredPane.AccessibleJLayeredPane
Provides accessibility support for
JLayeredPane . |
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field and Description |
---|---|
static Integer |
DEFAULT_LAYER |
static Integer |
DRAG_LAYER |
static Integer |
FRAME_CONTENT_LAYER |
static String |
LAYER_PROPERTY |
static Integer |
MODAL_LAYER |
static Integer |
PALETTE_LAYER |
static Integer |
POPUP_LAYER |
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
Constructor and Description |
---|
JLayeredPane() |
Modifier and Type | Method and Description |
---|---|
protected void |
addImpl(Component comp,
Object layerConstraint,
int index)
|
AccessibleContext |
getAccessibleContext()
Returns the accessible context for this
JLayeredPane . |
int |
getComponentCountInLayer(int layer)
Return the number of components within a layer of this
container.
|
Component[] |
getComponentsInLayer(int layer)
Return an array of all components within a layer of this
container.
|
protected Hashtable<Component,Integer> |
getComponentToLayer()
Return a hashtable mapping child components of this container to
Integer objects representing the component's layer assignments.
|
int |
getIndexOf(Component c)
Return the index of a component within the underlying (contiguous)
array of children.
|
int |
getLayer(Component c)
Looks up the layer a child component is currently assigned to.
|
static int |
getLayer(JComponent comp)
Looks up the layer in the client property with the key
LAYER_PROPERTY of comp . |
static JLayeredPane |
getLayeredPaneAbove(Component comp)
Returns the first JLayeredPane that contains the Component
comp or null if comp is
not contained in a JLayeredPane. |
protected Integer |
getObjectForLayer(int layer)
Return an Integer object which holds the same int value as the
parameter.
|
int |
getPosition(Component c)
Return the position of a component within its layer.
|
int |
highestLayer()
Return the greatest layer number currently in use, in this container.
|
protected int |
insertIndexForLayer(int layer,
int position)
Computes an index at which to request the superclass
Container inserts a component, given an abstract layer and
position number. |
boolean |
isOptimizedDrawingEnabled()
Returns
false if components in this layered pane can overlap,
otherwise true . |
int |
lowestLayer()
Return the least layer number currently in use, in this container.
|
void |
moveToBack(Component c)
Moves a component to the "back" of its layer.
|
void |
moveToFront(Component c)
Moves a component to the "front" of its layer.
|
void |
paint(Graphics g)
This method is overridden order to provide a reasonable painting
mechanism for
JLayeredPane . |
static void |
putLayer(JComponent component,
int layer)
Sets the layer property for a JComponent.
|
void |
remove(int index)
Removes a child from this container.
|
void |
removeAll()
Removes all components from this container.
|
void |
setLayer(Component c,
int layer)
Set the layer property for a component, within this container.
|
void |
setLayer(Component c,
int layer,
int position)
Set the layer and position of a component, within this container.
|
void |
setPosition(Component c,
int position)
Change the position of a component within its layer.
|
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, 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, isPaintingTile, isRequestFocusEnabled, isValidateRoot, 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
add, add, add, add, add, addContainerListener, 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, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, 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, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
public static final String LAYER_PROPERTY
public static final Integer FRAME_CONTENT_LAYER
public static final Integer DEFAULT_LAYER
public static final Integer PALETTE_LAYER
public static final Integer MODAL_LAYER
public static final Integer POPUP_LAYER
public static final Integer DRAG_LAYER
public JLayeredPane()
public int getLayer(Component c)
c
is an instance of JComponent
, then the layer
is fetched from the client property with the key LAYER_PROPERTY
.
Otherwise it is looked up in an internal hashtable that maps
non-JComponent components to layers. If the components cannot be found
in either way, the DEFAULT_LAYER
is returned.c
- the component to look up.public static int getLayer(JComponent comp)
LAYER_PROPERTY
of comp
. If no such property can be
found, we return 0
(DEFAULT_LAYER
).comp
- the component for which the layer is looked upcomp
as stored in the corresponding
client property, or 0
if there is no such propertypublic static JLayeredPane getLayeredPaneAbove(Component comp)
comp
or null
if comp
is
not contained in a JLayeredPane.comp
- the component for which we are searching the JLayeredPane
ancestorcomp
or null
if comp
is
not contained in a JLayeredPanepublic int highestLayer()
lowestLayer()
public int lowestLayer()
highestLayer()
public void moveToFront(Component c)
c
- the component to move to the front of its layermoveToBack(java.awt.Component)
public void moveToBack(Component c)
Moves a component to the "back" of its layer. The "back" is a synonym for position N-1 (also known as position -1), where N is the size of the layer.
The "back" of a layer is the first position drawn, so the component at the "back" is usually the component which is occluded by the most other components in its layer.
c
- the component to move to the back of its layer.moveToFront(java.awt.Component)
public int getPosition(Component c)
c
- the component to get the position ofc
within its layer or -1 if
c
is not a child of this layered panesetPosition(java.awt.Component, int)
public void setPosition(Component c, int position)
c
- the component to change the position ofposition
- the position to assign the component togetPosition(java.awt.Component)
public Component[] getComponentsInLayer(int layer)
layer
- the layer to return components frompublic int getComponentCountInLayer(int layer)
layer
- the layer count components inprotected Hashtable<Component,Integer> getComponentToLayer()
public int getIndexOf(Component c)
c
- the component to look up.-1
if
c
is not a child of this layered paneprotected Integer getObjectForLayer(int layer)
layer
- the layer number as an int.protected int insertIndexForLayer(int layer, int position)
Container
inserts a component, given an abstract layer and
position number.layer
- the layer in which to insert a component.position
- the position in the layer at which to insert a component.public void remove(int index)
public void removeAll()
public void setLayer(Component c, int layer)
Set the layer property for a component, within this container. The component will be implicitly mapped to the bottom-most position in the layer, but only if added after calling this method.
Read that carefully: this method should be called before the component is added to the container.
c
- the component to set the layer property for.layer
- the layer number to assign to the component.public void setLayer(Component c, int layer, int position)
c
- the child component to set the layer property for.layer
- the layer number to assign to the component.position
- the position number to assign to the component.protected void addImpl(Component comp, Object layerConstraint, int index)
Container
such that layerConstraint
is interpreted as an Integer
, specifying the layer to which the component will be added
(at the bottom position).
The argument index
specifies the position within the layer
at which the component should be added, where 0
is the top
position greater values specify positions below that and -1
specifies the bottom position.public static void putLayer(JComponent component, int layer)
component
- the component for which to set the layerlayer
- the layer property to setpublic AccessibleContext getAccessibleContext()
JLayeredPane
.getAccessibleContext
in interface Accessible
getAccessibleContext
in class JComponent
JLayeredPane
public void paint(Graphics g)
JLayeredPane
. This is necessary since
JLayeredPane
's do not have an own UI delegate.
Basically this method clears the background for the
JLayeredPane
and then calls super.paint(g)
.paint
in class JComponent
g
- the graphics context to useJComponent.paintImmediately(Rectangle)
public boolean isOptimizedDrawingEnabled()
false
if components in this layered pane can overlap,
otherwise true
.isOptimizedDrawingEnabled
in class JComponent
false
if components in this layered pane can overlap,
otherwise true