Forums Register Login
container's containment
I got this statement from sun website. I couldn't understand. could someone explain it,please.

Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.
a specific component can only be in one place at a time.

run this and the button will end up added to the final panel only

now move the creation of the button to inside the loop and it works OK,
because each button (same name) is a separate button
why is this happening to components that are different from each other.
This is my code excerpts.

public static void showFrame()
JFrame frame=new JFrame("Swing Test");

JMenuBar menu=new JMenuBar();
menu.setBackground(new Color(154,165,127));
menu.setPreferredSize(new Dimension(200, 20));

JLabel label=new JLabel();
label.setBackground(new Color(248, 213, 131));
label.setPreferredSize(new Dimension(200, 180));

JButton btn = new JButton("OK");
JPanel panel=new JPanel();
panel.setPreferredSize(new Dimension(100,50));



I have added two different component in the same container, but only the last added component is displayed in the frame. why?
Try changing your code to specify a location:

It works fine when i replaced mine with yours.so it is that we have to specify locations incase of multiple components. what if we are running out of locations.
> what if we are running out of locations.

never going to happen.

Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 844 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 18, 2018 19:19:51.