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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With