• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hidden or Removed

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
public class TestFrame extends Frame
{
Button firstOne = new Button("One");
Button secondOne = new Button("Two");
public TestFrame()
{
add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
When its compiled Two comes out
But what happens to One
is it Removed or Hidden?
 
Jim Petersen
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ragu Sivaraman:

add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);

Hi Ragu note when you use the BorderLayout you can add up to One component to any region(N,S,E,W, Center) so what happens is that when you attempt to and add another button to the to the North region you are effectively overwriting firstOne button with secondOne button only secondOne will be visible
All the best - Jim
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK the component is removed from the Frame container... It would not make sense to hide it since you can achieve the same result with a CardLayout and furthermore you could run out of memory if you were to continuously add components to the Frame and never remove them... This is unlikely to create so much components but who knows ?!
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look into the source of java/awt/BorderLayout.java in the method addLayoutComponent

where center, north, south, east and west are instances of Component, you may notice that those component are reassigned, which means the old one is not referenced anymore and may be gced soon...
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BorderLayout tracks which components to keep in each of the 5 areas using 5 fields which hold a reference to the component that they should display. When the second Button is assigned to the NORTH area, the field replaced the reference to the first, so it is lost forever - not just covered up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic