Java JLayeredPane

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java JLayeredPane

The JLayeredPane class is used to add depth to swing container. It is used to provide a third dimension for positioning component and divide the depth-range into several different layers.

JLayeredPane class declaration

  1. public class JLayeredPane extends JComponent implements Accessible  

Commonly used Constructors:

Constructor

Description

JLayeredPane

It is used to create a new JLayeredPane

Commonly used Methods:

Method

Description

int getIndexOf(Component c)

It is used to return the index of the specified Component.

int getLayer(Component c)

It is used to return the layer attribute for the specified Component.

int getPosition(Component c)

It is used to return the relative position of the component within its layer.

Java JLayeredPane Example

  1. import javax.swing.*;  
  2. import java.awt.*;  
  3. public class LayeredPaneExample extends JFrame {  
  4.   public LayeredPaneExample() {  
  5.     super("LayeredPane Example");  
  6.     setSize(200200);  
  7.     JLayeredPane pane = getLayeredPane();  
  8.     //creating buttons  
  9.     JButton top = new JButton();  
  10.     top.setBackground(Color.white);  
  11.     top.setBounds(20205050);  
  12.     JButton middle = new JButton();  
  13.     middle.setBackground(Color.red);  
  14.     middle.setBounds(40405050);  
  15.     JButton bottom = new JButton();  
  16.     bottom.setBackground(Color.cyan);  
  17.     bottom.setBounds(60605050);  
  18.     //adding buttons on pane  
  19.     pane.add(bottom, new Integer(1));  
  20.     pane.add(middle, new Integer(2));  
  21.     pane.add(top, new Integer(3));  
  22.   }  
  23.   public static void main(String[] args) {  
  24.       LayeredPaneExample panel = new  LayeredPaneExample();  
  25.       panel.setVisible(true);  
  26.   }  
  27. }  

Output:

Java Jlayeredpane 1