File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Layout Qstn Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Layout Qstn" Watch "Layout Qstn" New topic

Layout Qstn

Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
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());
setLayout(new BorderLayout());
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
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521

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.


"JavaRanch, where the deer and the Certified play" - David O'Meara
sajida kal
Ranch Hand

Joined: Mar 22, 2001
Posts: 89
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.

Cindy, do get back if you feel I am wrong.
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
In my feeling the answer should be 3 only.
[This message has been edited by tvs sundaram (edited May 11, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Folks,
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
Hope this issue is clear now.
Ravindra Mohan..
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Sathi Chowdhury
Ranch Hand

Joined: Mar 16, 2001
Posts: 52
thank you jane...
It is sorta covered in the JavaRanch Style Guide.
subject: Layout Qstn
It's not a secret anymore!