Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abhilash #39 - layout managers

 
Carmen Stuparu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can anyone explain this to me? I thought the answer was 3, not 5! Thanks in advance ...
import java.awt.*;
public class TestFrame extends Frame
{ Button bNorth = new Button("North");
Button bSouth = new Button("South");
Button bEast = new Button("East");
Button bWest = new Button("West");
Button bCenter = new Button("Center");
public TestFrame()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Attemping to compile and run the above code

1.Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager.
2.Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout Manager.
3.Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen.
4.Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager. 5.Will compile and run cleanly, but no component is visible.
6.Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
 
Lokesh Mahajan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Dear

The things works like this according to me.
after setting lay out to flowlayout you have added the componenet ,than you have not set its setvisible property to true.
Than you have set again the layout manager to borderlayout,here you set it visible property to true, but your border layout does not contain any componenet to display it,so it is showing blank screen.
love
Lokesh
 
Carmen Stuparu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lokesh
Thanks for your reply, but I'm still a little confused: changing the layout from border to grid, shoud lead to the same result (empty frame), right? But it doesn't!!
What am I missing here?
Gratefully,
Carmen
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carmen,
All containers maintain a list of the components they contain however, there are two types of LayoutManager interfaces. Layout managers which implement the <code>LayoutManager</code> interfaces ie FlowLayout, ask the container for a list of all it's components and then re-draws the container.
Layout managrs which implement the <code>LayoutManager2</code> interfaces ie BorderLayout, maintain an internal list of the containers components. They do not ask the container for a list of contained components.
In the example, when the layout is changed to BorderLayout it has an empty list of components so nothing is displayed.
If you reverse the order in which the layouts are assigned ie assign BorderLayout first, add the buttons, then switch to FlowLayout you'll see that FlowLayout will display the buttons because it queries the Frame.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jane,
That's good info, thanks. In short FlowLayout() can ask for components from the container and BorderLayout() cannot. If the container layout is reset to BorderLayout() then the components must be readded. Does this about sum it up?
Kevin
 
Carmen Stuparu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane!
Thanks everybody!
Carmen
Another SCJP (3 hours ago)

 
Rosie Vogel
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Carmen, way to go! Congratulations!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic