• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Layout Qstn

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read the code below
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.
The Answer is given as 5.
I dont understand why,i thougt it should be 3.
folks help me out
thanks
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Note that when using the BorderLayout, you must add components with either the add(String, Component) or the add(Component, Object) method to ensure the components are all added correctly. A common mistake people make is to use add(Component), which can result in some of the components not being visible.


From: http://developer.java.sun.com/developer/technicalArticles/GUI/AWTLayoutMgr/
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cindy,
I think you might be mistaken. This question is more to do with the behaviour of layout managers when layout managers are changed after components are added. Do correct me if I am wrong.
Sathi, I had the same doubt yesterday and did a search on layout
in this forum. There is a thread titled Layout Manager-mock exam by Cristi Trudose, posted in Jan 2001. Check it out. There was a lot of discussion and finally Jane found something which might help you. I am posting that response here-


Well I was poking around in another book last night (Java Algorithms by Scott Robert Ladd) I think I found the right answer
Basically, Containers keep track of the Components they contain but it is the LayoutManager that actually handles the positioning of the Components. Each Container can be associated with only one LayoutManager or have a null LayoutManager which means you have to supply the code to handle positioning and resizing separately.
LayoutManagers contain a number of methods: addLayoutComponent(), layoutContainer(), etc. However, in most situations you don't call these directly. Instead, you use the Container methods which in turn call the LayoutManager methods ie Container.add() calls LayoutManager addLayoutComponent().
The key to the difference between LayoutManagers which require constraints such as BorderLayout, and those which don't have constraints such as FlowLayout is this:
Classes implementing LayoutManager2 ie BorderLayout, CardLayout and GridBagLayout maintain an internal list of their components.
Classes implementing LayoutManager ie FlowLayout, GridLayout query the target container for a list of Components
This is why we see the behaviour shown in the original example. When components are added to a Container, the Container itself keeps a list of the objects. If, for example, the Layout has been set to FlowLayout, and the Container is resized, the FlowLayout manager will get a list of the component objects associated with the Container and then perform the size and position functions.
If the Layout is then changed to, for example, BorderLayout, and the Container is resized, no request is made to the Container for a list of the associated Components. The BorderLayout contract states it will keep it's own list of Components; as far as it's concerned, it has no components and nothing needs to be positioned.
This is why the Components need to be re-added to the Container.
Hope thats clearer.
Jane


Cindy, do get back if you feel I am wrong.
Regards
Sajida
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my feeling the answer should be 3 only.
[This message has been edited by tvs sundaram (edited May 11, 2001).]
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks,
When in DOUBT RELY ON THE BEST TEACHER...THE COMPILER.
Ofcource, there is no doubt that nothing will be visible.
Run the code and get the answer.
As far the reason is concerned, I agree with Cindy, please
visit the link to get the answer yourself.
Click to see http://developer.java.sun.com/developer/technicalArticles/GUI/AWTLayoutMgr/#BORDER
Hope this issue is clear now.
Ravindra Mohan..
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is obviously showing no component..but I found that only Borderlayout is behaving this way,when we are changing the Layout for the 2nd time,if we set the layout to any other layout except BorderLayout ,then the components are rearranged and repositioned according to the later one and displayed.
I tried with diff combination,if for the first time the layout was BorderLayout and u add the cmponents and then again change it to BorderLayout then also the same result,no comp are shown.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sathi,
You'll find that any layout which implements the <code>LayoutManager2</code> interface will behave this way ie CardLayout, BorderLayout, GridBagLayout, BoxLayout, and OverlayLayout.
These layouts keep their own lists of components. They do not query the container. Any components added to a container prior to a <code>setLayout</code> statement for a LayoutManger2 layout will be ignored.
Layouts which implement the <code>LayoutManager</code> interface ie GridLayout, FlowLayout, ViewportLayout, or ScrollPaneLayout query the container for a list of components; they will always display any component the container has.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Sathi Chowdhury
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you jane...
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic