• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head First Java page 415 - Question on modified behavior  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is from the book and works fine.
As a test I wanted to play with the layout to see the behavior.
To keep it simple, I added the same button to the NORTH panel on the layout.

frame.getContentPane().add(BorderLayout.NORTH, button); <<<------Added this line
frame.getContentPane().add(BorderLayout.SOUTH, button); <<<------This was the original line
frame.getContentPane().add(BorderLayout.CENTER, panel);

I was expecting the same button to be in two locations (NORTH and SOUTH), but instead it only appear in which ever is last.
If SOUTH is first and NORTH after then the button appears NORTH.

Granted it doesn't make sense for two buttons to exist on the same frame but am still curious as to why it doesn't work.
I would expect the NORTH and SOUTH panel to essentially reference the same object in memory so why can't both be seen?


 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just found the following :

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.


Apparently it is a "rule". I was just wondering if there was some special implementation.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!