Question

I have a JFrame where it uses a background Image as the content pane. I successfully did it my having an ImagePanel class. However, when I try to add others components, these components do not show. I guess that it is about the Z order so I tried to set the Z order of the label but it gave me an error.

frame.setContentPane(new ImagePanel(bg));
frame.setBackground(new Color(0,255,0,0));
frame.getContentPane().setComponentZOrder(jLabel1, 1);
frame.setVisible(true);

The exception is:

java.lang.IllegalArgumentException: illegal component position

When I try to change the Z order to 0, it gives me a different error

frame.getContentPane().setComponentZOrder(jLabel1, 0);

the error is:

java.lang.IllegalArgumentException: component and container should be in the same top-level window
Was it helpful?

Solution

You get the Exception because you haven't add the label to the frame. However, using ZOorder is not the way to solve your problem.

Instead you should be adding the JLabel to the ImagePanel.

ImagePanel panel = new ImagePanel(bg);
panel.setLayout(...);
panel.add(label);
frame.setContentPane(panel);

OTHER TIPS

You basically never want to replace the component pane of a frame, but add components to it:

frame.add(jLabel1); // I assume this is included in your code
ImagePanel image = new ImagePanel(bg)
frame.add(image);
frame.getContentPane().setComponentZOrder(jLabel1, 1);
frame.getContentPane().setBackground(new Color(0,255,0,0));
frame.setVisible(true);

I also added some .getContentPane() to make sure that all methods are relative to the content pane, and not the frame, as not all methods are redirected.

Please read the Javadocs of JFrame and JFrame.setContentPane for further information.

setComponentZOrder can't be set higher than the number of components you have in the frame - 1.

So, make sure you have at least 2 components in the frame before setting the Z order to 1.

You have not posted full code to find issue, but here is small demo of Z-order:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class NewClass extends JFrame {
    public NewClass(){
        super("Test");
        setSize(200, 200);

        JPanel panel = new JPanel();
        setContentPane(panel);

        JButton button1 = new JButton("Button 1");
        JButton button2 = new JButton("Button 2");
        JButton button3 = new JButton("Button 3");

        button1.setBounds(10, 10, 100, 40);
        button2.setBounds(5, 5, 100, 30);
        button3.setBounds(15, 15, 150, 40);

        panel.setLayout(null);

        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        panel.setComponentZOrder(button1, 1);
        panel.setComponentZOrder(button2, 0);
        panel.setComponentZOrder(button3, 2);

        // OR to swap z order of buttons, try below
        // panel.setComponentZOrder(button1, 0);
        // panel.setComponentZOrder(button2, 1);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String [] args){
        new NewClass();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top