Updated March 18, 2023
Introduction to JLayeredPane
JLayeredPane is a Swing component that enables us to add components to a different layer. A common example of a “different layer” is a pop-up JLayeredPane. When we must show some dialog, warning or information, we use a pop-up window. This can be achieved using JLayeredPane.
Declaration:
public class JLayeredPane extends JComponent implements Accessible
Layers of JLayeredPane
Here are some layers of JLayeredPane as explained below in detail:
- DEFAULT_LAYER: This is the standard and bottommost layer, where most components are inserted.
- PALETTE_LAYER: This layer of JLayeredPanesits over the default layer and useful for floating toolbars and palettes.
- MODAL_LAYER: This layer of JLayeredPane is used for model dialogs and shown above the palette layer.
- POPUP_LAYER: This layer of JLayeredPaneis used to show popup windows above the modal layer.
- DRAG_LAYER: A component is shown in this layer (above the popup layer) while dragging it. When the dragging is finished, the component is shown in its original layer.
Constructor Details: This is the only constructor that creates a JLayeredPane.
public JLayeredPane()
Method Details of JLayeredPane
Here are some methods that we use :
1. protected void addImpl(Component comp, Object constraints, int index): This method adds a component to this container at the specified index. Also, LayoutManager is notified to add the specified component to this container’s layout using the specified constraints object. For this addLayoutComponent method is used.
2. public void remove(int index): This method removes a component from the specified index of this pane.
3. public void removeAll(): Removes all the components from this pane.
4. public boolean isOptimizedDrawingEnabled(): If layers in the pane can overlap, it makes optimized drawing impossible and returns false. Otherwise true.
5. public static void putLayer(JComponent c, int layer): This method sets the layer for a JComponent. This method does not cause any side effects like paint, add, remove, etc. To have these side effects, you can use the setLayer() method.
6. public static int getLayer(JComponent c): This method returns the layer attribute of specified JComponent.
7. public static JLayeredPane getLayeredPaneAbove(Component c): This method returns the first JLayeredPane which contains the specified component or returns null if no JLayeredPane is found.
8. public void setLayer(Component c, int layer): This method sets the JLayeredPane attribute of the specified component. It makes the component bottommost in the specified layer.
9. public void setLayer(Component c, int layer, int position): This method sets the layer attribute of the specified component and sets the position of the component in that layer.
10. public int getLayer(Component c): This method returns the layer attribute of the specified Component.
11. public int getIndexOf(Component c): This method returns the index of the specified component in the pane ignoring the layers. The topmost component is at index zero and the bottommost component is at the highest index.
12. public void moveToFront(Component c): This method moves the specified component to the top in its current layer (position 0).
13. public void moveToBack(Component c): This method moves the specified component to the bottom in its current layer (position -1).
14. public void setPosition(Component c, int position): This method sets the position of the specified component within its layer where 0 is the topmost position and –1 is the bottom-most position.
15. public int getPosition(Component c): This method returns the relative position of the component within its layer.
16. public int highestLayer(): This method returns the value of the highest layer from all current children. If there are no children, it returns 0.
17. public int lowestLayer(): This method returns the value of the lowest layer from all current children. If there are no children, it returns 0.
18. public int getComponentCountInLayer(int layer): This method returns the number of children currently present in the layer specified.
19. public Component[] getComponentsInLayer(int layer): This method returns the components in an array present in the layer specified.
20. public void paint(Graphics g): This method paints this JLayeredPane within the specified graphics context.
21. protected Hashtable<Component,Integer> getComponentToLayer(): This method returns a HashTable which maps components to layers.
22. protected Integer getObjectForLayer(int layer): This method returns the integer associated with the layer specified.
23. protected int insertIndexForLayer(int layer, int position): This method determines and returns the proper location to insert a new child based on the layer and position specified.
24. protected String paramString(): This method returns the string representation of this pane. This method is used internally for debugging purposes.
25. public AccessibleContext getAccessibleContext(): This method returns the AccessibleContext associated with this pane.
Example of JLayeredPane
Here is an example given below with explanation:
Code:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
public class JLayeredPaneDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JLayeredPane Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(520, 530);
JLayeredPane jLayeredPane = new JLayeredPane();
JButton jButton1 = new JButton("Open");
jButton1.setBackground(Color.green);
jButton1.setBounds(175, 50, 150, 100);
jLayeredPane.add(jButton1, JLayeredPane.DEFAULT_LAYER);
JButton jButton2 = new JButton("Open");
JButton jButton3 = new JButton("Close");
jButton2.setVisible(false);
jButton3.setVisible(false);
jButton2.setBackground(Color.RED);
jButton3.setBackground(Color.RED);
jButton2.setBounds(75, 125, 150, 100);
jButton3.setBounds(275, 125, 150, 100);
jLayeredPane.add(jButton2, JLayeredPane.PALETTE_LAYER);
jLayeredPane.add(jButton3, JLayeredPane.PALETTE_LAYER);
JButton jButton4 = new JButton("Open");
JButton jButton5 = new JButton("Close");
jButton4.setVisible(false);
jButton5.setVisible(false);
jButton4.setBackground(Color.MAGENTA);
jButton5.setBackground(Color.MAGENTA);
jButton4.setBounds(95, 200, 150, 100);
jButton5.setBounds(255, 200, 150, 100);
jLayeredPane.add(jButton4, JLayeredPane.MODAL_LAYER);
jLayeredPane.add(jButton5, JLayeredPane.MODAL_LAYER);
JButton jButton6 = new JButton("Open");
JButton jButton7 = new JButton("Close");
jButton6.setVisible(false);
jButton7.setVisible(false);
jButton6.setBackground(Color.CYAN);
jButton7.setBackground(Color.CYAN);
jButton6.setBounds(75, 275, 150, 100);
jButton7.setBounds(275, 275, 150, 100);
jLayeredPane.add(jButton6, JLayeredPane.POPUP_LAYER);
jLayeredPane.add(jButton7, JLayeredPane.POPUP_LAYER);
JButton jButton8 = new JButton("Close");
jButton8.setVisible(false);
jButton8.setBackground(Color.GRAY);
jButton8.setBounds(175, 350, 150, 100);
jLayeredPane.add(jButton8, JLayeredPane.DRAG_LAYER);
frame.add(jLayeredPane);
frame.setVisible(true);
jButton1.addActionListener(e -> {
jButton2.setVisible(true);
jButton3.setVisible(true);
});
jButton2.addActionListener(e -> {
jButton4.setVisible(true);
jButton5.setVisible(true);
});
jButton3.addActionListener(e -> {
jButton2.setVisible(false);
jButton3.setVisible(false);
});
jButton4.addActionListener(e -> {
jButton6.setVisible(true);
jButton7.setVisible(true);
});
jButton5.addActionListener(e -> {
jButton4.setVisible(false);
jButton5.setVisible(false);
});
jButton6.addActionListener(e -> {
jButton8.setVisible(true);
});
jButton7.addActionListener(e -> {
jButton6.setVisible(false);
jButton7.setVisible(false);
});
jButton8.addActionListener(e -> {
jButton8.setVisible(false);
});
}
}
Explanation
In this example, we have implemented a program that shows different layers of JLayeredPane and how to insert/remove components from the pane.
- First, we have created a frame and added a JLayeredPane to it.
- Then added buttons in different layers of the JLayeredPane.
- Attached action listeners to the buttons to show or hide the next layer.
- The position of the buttons with the layer is set so that it looks properly.
Output:
1. This is our application and this green button is added to the bottommost layer.
2. When a user clicks on a green button from the first layer, the second layer is opened.
3. When clicked on the open button in the second layer, the third layer in JLayeredPane is opened.
4. When clicked on the open button in the third layer, the fourth layer is opened.
5. When clicked on the open button in the fourth layer, the fifth layer is opened.
6. When clicked on the close button in the fifth layer, the layer of JLayeredPane is closed.
7. When clicked on the close button in the fourth layer, the layer of JLayeredPane is closed.
8. When clicked on the close button in the third layer, the layer of JLayeredPane is closed.
9. When clicked on the close button in the second layer, the layer is closed.
Conclusion
JLayeredPane is easy to use and an important component in Swing. It makes it easy to decide among components while overlapping situations.
Recommended Articles
This is a guide to JLayeredPane. Here we discuss the layers, constructor, and methods of JLayeredPane along with example and its code implementation. You may also look at the following articles to learn more –