VOOZH about

URL: https://www.geeksforgeeks.org/java/java-jlayeredpane/

⇱ Java JLayeredPane - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java JLayeredPane

Last Updated : 28 Apr, 2025

In Java Swing, JLayeredPane is a powerful container that allows you to overlay and manage multiple components within a single container. Each component in a JLayeredPane can occupy a different layer, and you can control their positions and visibility. In this article, we are going to implement the JLayeredPane with different examples. In Java JLayeredPane is a part of javax.swing package.

Default Constructor used for JLayeredPane class

JLayeredPane() : It is the default constructor of JLayeredPane, It is used to create a new JLayeredPane.

Example of JLayeredPane

//Creating an object of JLayeredPane
JLayeredPane layeredPane = new JLayeredPane();

Some Methods of the JLayeredPane

Methods

Description

int getIndexOf(Component com)

Method for returning the index of the specified Component.

int getPosition(Component com)

Method used to return the appropriate position of the component within the layer

int getLayer(Component com)

Method for returning the layer attribute for the specific Component.

add(Component com, Integer layer)

Method for returning the relative attribute for a specific component.

moveToFront(Component com)

Navigate to the Front layer

moveToBack(Component com)

Navigate to the backward layer.

Name of the Classes from which methods are inherited

  1. javax.swing.JComponent
  2. java.awt.Container
  3. java.awt.Component
  4. java.awt.event.MouseListener
  5. java.awt.event.MouseMotionListener
  6. java.awt.event.FocusListener
  7. java.awt.event.KeyListener

Following are the programs to implement JLayeredPane

Example 1:

Java Program to demonstrate a simple JLayeredPane:

Output:

👁 Output of JLayeredPane Example 1

Example 2:

Implementing getIndexOf(Component com), getLayer(Component com), and getPosition(Component com) methods in JLayeredPane to get a specified layer information.

Below is the implementation of the above example:

Comment