Question

I am trying to add to controls (JPanel and JLabel) on JFrame using a JLayeredPane.

Label is having the background image with the help of ImageIcon and panel is having button controls on it. It's showing the jlabel with background image

But panel controls are not displaying. I am using the following code.

   try
  {
  JLayeredPane layers= new JLayeredPane();
  ImageIcon img1= ImageIcon("path upto image");
  JLabel l1= new JLabel("");
  l1.setIcon(img1);


  JPanel panel1   = new JPanel();

   layers.add(l1);
  layers.add(panel1);

  add(layers);
 }
catch(Exception ex){ex.printStackTrace();}

How to display the panel controls?

Was it helpful?

Solution

By default, components don't have position or size, meaning if you add them to a container that has no layout manager (as is the case with JLayeredPane), they will not appear.

Because JLayeredPane has no layout manager, you must take over the responsibility of provide position and size information to your components.

Try using setSize, setLocation and/or setBounds.

Take a look at the examples from How to use layered panes for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top