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
- javax.swing.JComponent
- java.awt.Container
- java.awt.Component
- java.awt.event.MouseListener
- java.awt.event.MouseMotionListener
- java.awt.event.FocusListener
- java.awt.event.KeyListener
Following are the programs to implement JLayeredPane
Example 1:
Java Program to demonstrate a simple JLayeredPane:
// Java Program to demonstrate a simple JLayeredPane import javax.swing.*;
import java.awt.*;
public class LayeredPaneExample {
public static void main(String[] args) {
// Create a JFrame (the main window for the application).
JFrame frame = new JFrame( "JLayeredPane Example" );
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 400 , 400 );
// Create a JLayeredPane to manage the layering of components.
JLayeredPane layeredPane = new JLayeredPane();
frame.add(layeredPane); // Add the JLayeredPane to the JFrame.
// Create three colored panels to add to the layered pane.
JPanel panel1 = createColoredPanel(Color.RED, 100 , 100 , 200 , 200 );
JPanel panel2 = createColoredPanel(Color.GREEN, 150 , 150 , 200 , 200 );
JPanel panel3 = createColoredPanel(Color.BLUE, 200 , 200 , 200 , 200 );
// Add the panels to the layered pane with different layer values.
// The layers determine the stacking order of the panels.
layeredPane.add(panel1, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(panel2, JLayeredPane.PALETTE_LAYER);
layeredPane.add(panel3, JLayeredPane.MODAL_LAYER);
frame.setVisible( true ); // Make the JFrame visible.
}
private static JPanel createColoredPanel(Color color, int x, int y, int width, int height) {
// Create a colored JPanel with specified color and position.
JPanel panel = new JPanel();
panel.setBackground(color);
panel.setBounds(x, y, width, height);
return panel;
}
} |
Output:
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:
// Java Swing Program to Implementing // getIndexOf(Component com), getLayer(Component com) // and getPosition(Component com) methods in // JLayeredPane to get a specified layer information import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
// Driver Class public class LayeredPaneMethodsExample {
// main function
public static void main(String[] args)
{
JFrame frame = new JFrame( "JLayeredPane Example" );
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setSize( 400 , 400 );
// Creating an object of JLayeredPane
JLayeredPane layeredPane = new JLayeredPane();
frame.add(layeredPane); // Adding Frame to it
// Create three colored panels
JPanel panel1 = createColoredPanel(Color.RED, 100 ,
100 , 200 , 200 );
JPanel panel2 = createColoredPanel(Color.GREEN, 150 ,
150 , 200 , 200 );
JPanel panel3 = createColoredPanel(Color.BLUE, 200 ,
200 , 200 , 200 );
// Add the panels to the layered pane with different
// layer values
layeredPane.add(panel1, JLayeredPane.DEFAULT_LAYER);
layeredPane.add(panel2, JLayeredPane.PALETTE_LAYER);
layeredPane.add(panel3, JLayeredPane.MODAL_LAYER);
// Creating a JButton by clicking which we get layer
// information
JButton getInfoButton = new JButton(
"Click here to get Layer Information" );
getInfoButton.setBounds( 10 , 10 , 100 , 30 );
frame.add(getInfoButton);
getInfoButton.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// Get information about the second
// panel (panel2)
int index = layeredPane.getIndexOf(panel2);
int layer = layeredPane.getLayer(panel2);
int position = layeredPane.getPosition(panel2);
JOptionPane.showMessageDialog( null , "Index: " + index
+ "\nLayer: " + layer + "\nPosition: " + position);
}
});
frame.setVisible( true );
}
private static JPanel createColoredPanel(Color color, int x, int y, int width, int height)
{
JPanel panel = new JPanel();
panel.setBackground(color);
panel.setBounds(x, y, width, height);
return panel;
}
} |