CrazyProgrammer
CrazyProgrammer

Reputation: 544

JPanel z-ordering

I have one base JPanel on which I will place other JPanel items. I want to show one JPanel on top of the other JPanel item.

I tried with the code below, but it did not work.

package test;

import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JPanelParent extends JFrame
{

    public JPanelParent() throws HeadlessException {
        JPanel nPanel = getNode(10,10,20,20, Color.red,1);
        JPanel nPanel2 = getNode(20,20,20,20, Color.yellow,2);
        JPanel nPanel3 = getNode(30,30,20,20, Color.green,3);
        add(nPanel);
        add(nPanel2);
        add(nPanel3);
    }

    public JPanel getNode(int i, int i1, int j, int j1, Color color, int zOrder){
        JPanel nodePanel = new JPanel();
        nodePanel.setBackground(color);
        nodePanel.setBounds(i,i1,j,j1);
        nodePanel.setOpaque(true);
        nodePanel.setLayout(null);
        return nodePanel;
    }


    public static void main(String argv[]){
        JPanelParent jpp = new JPanelParent();
        jpp.setVisible(true);
        jpp.setSize(300,300);
        jpp.setBackground(Color.black);
    }
}

Thanks

Upvotes: 2

Views: 1057

Answers (2)

Salah
Salah

Reputation: 8657

If understand your question, then first before you add your JPanels into parent frame, you need to tell the parent frame to add the component in a specific coordinates, and could be done by set the Layout for the parent frame with null.

add this line at the first line in your constructor.

setLayout(null);

Upvotes: 2

Paul Samsotha
Paul Samsotha

Reputation: 208984

You can use a JLayeredPane. See more How to Use JLayeredPane

public JPanelParent() throws HeadlessException {
    JPanel nPanel = getNode(10, 10, 20, 20, Color.red, 1);
    JPanel nPanel2 = getNode(20, 20, 20, 20, Color.yellow, 2);
    JPanel nPanel3 = getNode(30, 30, 20, 20, Color.green, 3);
    JLayeredPane pane = new JLayeredPane();
    pane.add(nPanel, new Integer(1));
    pane.add(nPanel2, new Integer(2));
    pane.add(nPanel3, new Integer(3));

    setContentPane(pane);
    getContentPane().setBackground(Color.BLACK);
}

Note: the Integer value passed is the layer order. The higher the number, the higher on top the layer

enter image description here

import java.awt.Color;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JPanelParent extends JFrame {

    public JPanelParent() throws HeadlessException {
        JPanel nPanel = getNode(10, 10, 20, 20, Color.red, 1);
        JPanel nPanel2 = getNode(20, 20, 20, 20, Color.yellow, 2);
        JPanel nPanel3 = getNode(30, 30, 20, 20, Color.green, 3);
        JLayeredPane pane = new JLayeredPane();
        pane.add(nPanel, new Integer(1));
        pane.add(nPanel2, new Integer(2));
        pane.add(nPanel3, new Integer(3));

        setContentPane(pane);
        getContentPane().setBackground(Color.BLACK);
    }

    public JPanel getNode(int i, int i1, int j, int j1, Color color, int zOrder) {
        JPanel nodePanel = new JPanel();
        nodePanel.setBackground(color);
        nodePanel.setBounds(i, i1, j, j1);
        nodePanel.setOpaque(true);
        nodePanel.setLayout(null);
        return nodePanel;
    }

    public static void main(String argv[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JPanelParent jpp = new JPanelParent();
                jpp.setVisible(true);
                jpp.setSize(300, 300);
                jpp.setBackground(Color.black);
            }
        });

    }
}

Side Note

  • Swing programs should be run from the Event Dispatch Thread (as I've done in the example above). See more at Initial Threads

Upvotes: 4

Related Questions