ImageObserver
, MenuContainer
, Serializable
, Accessible
JDesktopPane
@JavaBean(defaultProperty="accessibleContext") public class JLayeredPane extends JComponent implements Accessible
JLayeredPane
adds depth to a JFC/Swing container, allowing components to overlap each other when needed. An Integer
object specifies each component's depth in the container, where higher-numbered components sit "on top" of other components. For task-oriented documentation and examples of using layered panes see How to Use a Layered Pane, a section in The Java Tutorial. Example:
JLayeredPane
divides the depth-range into several different layers. Putting a component into one of those layers makes it easy to ensure that components overlap properly, without having to worry about specifying numbers for specific depths: JLayeredPane
methods moveToFront(Component)
, moveToBack(Component)
and setPosition
can be used to reposition a component within its layer. The setLayer
method can also be used to change the component's current layer. JLayeredPane
manages its list of children like Container
, but allows for the definition of a several layers within itself. Children in the same layer are managed exactly like the normal Container
object, with the added feature that when children components overlap, children in higher layers display above the children in lower layers. Each layer is a distinct integer number. The layer attribute can be set on a Component
by passing an Integer
object during the add call.
For example:
layeredPane.add(child, JLayeredPane.DEFAULT_LAYER); or layeredPane.add(child, Integer.valueOf(10));The layer attribute can also be set on a Component by calling
layeredPaneParent.setLayer(child, 10)on the
JLayeredPane
that is the parent of component. The layer should be set before adding the child to the parent. Higher number layers display above lower number layers. So, using numbers for the layers and letters for individual components, a representative list order would look like this:
5a, 5b, 5c, 2a, 2b, 2c, 1awhere the leftmost components are closest to the top of the display.
A component can be moved to the top or bottom position within its layer by calling moveToFront
or moveToBack
.
The position of a component within a layer can also be specified directly. Valid positions range from 0 up to one less than the number of components in that layer. A value of -1 indicates the bottommost position. A value of 0 indicates the topmost position. Unlike layer numbers, higher position values are lower in the display.
Note: This sequence (defined by java.awt.Container) is the reverse of the layer numbering sequence. Usually though, you will useHere are some examples using the method add(Component, layer, position): Calling add(5x, 5, -1) results in:moveToFront
,moveToBack
, andsetLayer
.
5a, 5b, 5c, 5x, 2a, 2b, 2c, 1aCalling add(5z, 5, 2) results in:
5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1aCalling add(3a, 3, 7) results in:
5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1aUsing normal paint/event mechanics results in 1a appearing at the bottom and 5a being above all other components.
Note: that these layers are simply a logical construct and LayoutManagers will affect all child components of this container without regard for layer settings.
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 XMLEncoder
.
Modifier and Type | Class | Description |
---|---|---|
protected class |
JLayeredPane.AccessibleJLayeredPane |
This class implements accessibility support for the JLayeredPane class. |
JComponent.AccessibleJComponent
Container.AccessibleAWTContainer
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
Modifier and Type | Field | Description |
---|---|---|
static final Integer |
DEFAULT_LAYER |
Convenience object defining the Default layer. |
static final Integer |
DRAG_LAYER |
Convenience object defining the Drag layer. |
static final Integer |
FRAME_CONTENT_LAYER |
Convenience object defining the Frame Content layer. |
static final String |
LAYER_PROPERTY |
Bound property |
static final Integer |
MODAL_LAYER |
Convenience object defining the Modal layer. |
static final Integer |
PALETTE_LAYER |
Convenience object defining the Palette layer. |
static final Integer |
POPUP_LAYER |
Convenience object defining the Popup layer. |
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 | Description |
---|---|
JLayeredPane() |
Create a new JLayeredPane |
Modifier and Type | Method | Description |
---|---|---|
AccessibleContext |
getAccessibleContext() |
Gets the AccessibleContext associated with this JLayeredPane. |
int |
getComponentCountInLayer |
Returns the number of children currently in the specified layer. |
Component[] |
getComponentsInLayer |
Returns an array of the components in the specified layer. |
protected Hashtable |
getComponentToLayer() |
Returns the hashtable that maps components to layers. |
int |
getIndexOf |
Returns the index of the specified Component. |
int |
getLayer |
Returns the layer attribute for the specified Component. |
static int |
getLayer |
Gets the layer property for a JComponent, it does not cause any side effects like setLayer(). |
static JLayeredPane |
getLayeredPaneAbove |
Convenience method that returns the first JLayeredPane which contains the specified component. |
protected Integer |
getObjectForLayer |
Returns the Integer object associated with a specified layer. |
int |
getPosition |
Get the relative position of the component within its layer. |
int |
highestLayer() |
Returns the highest layer value from all current children. |
protected int |
insertIndexForLayer |
Primitive method that determines the proper location to insert a new child based on layer and position requests. |
boolean |
isOptimizedDrawingEnabled() |
Returns false if components in the pane can overlap, which makes optimized drawing impossible. |
int |
lowestLayer() |
Returns the lowest layer value from all current children. |
void |
moveToBack |
Moves the component to the bottom of the components in its current layer (position -1). |
void |
moveToFront |
Moves the component to the top of the components in its current layer (position 0). |
void |
paint |
Paints this JLayeredPane within the specified graphics context. |
protected String |
paramString() |
Returns a string representation of this JLayeredPane. |
static void |
putLayer |
Sets the layer property on a JComponent. |
void |
remove |
Remove the indexed component from this pane. |
void |
removeAll() |
Removes all the components from this container. |
void |
setLayer |
Sets the layer attribute on the specified component, making it the bottommost component in that layer. |
void |
setLayer |
Sets the layer attribute for the specified component and also sets its position within that layer. |
void |
setPosition |
Moves the component to position within its current layer, where 0 is the topmost position within the layer and -1 is the bottommost position. |
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, 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, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, 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, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
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, 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, 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 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 static final Integer FRAME_CONTENT_LAYER
public static final String LAYER_PROPERTY
public JLayeredPane()
public void remove(int index)
public void removeAll()
@BeanProperty(bound=false) public boolean isOptimizedDrawingEnabled()
isOptimizedDrawingEnabled
in class JComponent
public static void putLayer(JComponent c, int layer)
c
- the JComponent to movelayer
- an int specifying the layer to move it topublic static int getLayer(JComponent c)
c
- the JComponent to checkpublic static JLayeredPane getLayeredPaneAbove(Component c)
c
- the Component to checkpublic void setLayer(Component c, int layer)
c
- the Component to set the layer forlayer
- an int specifying the layer to set, where lower numbers are closer to the bottompublic void setLayer(Component c, int layer, int position)
c
- the Component to set the layer forlayer
- an int specifying the layer to set, where lower numbers are closer to the bottomposition
- an int specifying the position within the layer, where 0 is the topmost position and -1 is the bottommost positionpublic int getLayer(Component c)
c
- the Component to checkpublic int getIndexOf(Component c)
c
- the Component to checkpublic void moveToFront(Component c)
c
- the Component to movepublic void moveToBack(Component c)
c
- the Component to movepublic void setPosition(Component c, int position)
position
within its current layer, where 0 is the topmost position within the layer and -1 is the bottommost position. Note: Position numbering is defined by java.awt.Container, and is the opposite of layer numbering. Lower position numbers are closer to the top (0 is topmost), and higher position numbers are closer to the bottom.
c
- the Component to moveposition
- an int in the range -1..N-1, where N is the number of components in the component's current layerpublic int getPosition(Component c)
c
- the Component to checkpublic int highestLayer()
public int lowestLayer()
public int getComponentCountInLayer(int layer)
layer
- an int specifying the layer to checkpublic Component[] getComponentsInLayer(int layer)
layer
- an int specifying the layer to checkpublic void paint(Graphics g)
paint
in class JComponent
g
- the Graphics context within which to paintprotected Hashtable<Component,Integer> getComponentToLayer()
protected Integer getObjectForLayer(int layer)
layer
- an int specifying the layerprotected int insertIndexForLayer(int layer, int position)
layer
- an int specifying the layerposition
- an int specifying the position within the layerprotected String paramString()
null
.paramString
in class JComponent
@BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
getAccessibleContext
in interface Accessible
getAccessibleContext
in class Component
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/javax/swing/JLayeredPane.html