Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting Component Z Order results to illegal position

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
like image 644
dgzz Avatar asked Jan 30 '26 17:01

dgzz


1 Answers

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);
like image 61
camickr Avatar answered Feb 02 '26 09:02

camickr