Forums Register Login

Urgent help please on AWT - mixing layout managers

+Pie Number of slices to send: Send
Abilash's mock exam Q39 has me confused. After Q38 I thought I had a decent idea of what was going on. But why doesn't any button appear when you construct a frame with this code:


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 BorderLayout());
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bNorth,BorderLayout.NORTH);
add(bCenter);
setLayout(new FlowLayout());
validate();
pack();
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}


I thought it would display a single button - the last one added - in the center of the frame, occupying the entire frame. But it doesn't. I obviously dont understand how and when doLayout() is called and components are laid out.
It is tough finding information on AWT. Can someone please give me some info/pointers?
[ May 12, 2002: Message edited by: R Arun ]
[ May 12, 2002: Message edited by: R Arun ]
+Pie Number of slices to send: Send
Only the Border and Card AWT LayoutManageres don't know how to relayout the components added previously to other LayoutManager. Thus in the example you see all the botons.

this is the old old AWT tutorial from SUN
Also in bruceeckel.com you can download the first edition of Thinking in Java.
Richard G Balwin has written a lot here
+Pie Number of slices to send: Send
Thanks for the quick response Jose.
+Pie Number of slices to send: Send
Arun,
All containers (ie Frame) have an internal list of all components added to them. Component on this list may also have a constraint object attached to them, which will be used by some layout managers to lay out the components.
When you add the components with the BorderLayout manager, it will attach a certain constraint to each component (NORTH, SOUTH, EAST...). At "lay out" time, the BorderLayout manager will check those constraints to decide where to place each component.
When you change layout manager, the container retains all components previously added (not quite sure what happens to the constraints though). In the case of FlowLayout, it will lay out all the componets of the container (ignoring the constraints property). You can check that the same applies for GridLayout.
The problem comes when you set a layout manager that uses constraints (such as BorderLayout). If the constraints are not properly set, this layout will produce strange results.

Eduard
and POOF! You're gone! But look, this tiny ad is still here:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 609 times.
Similar Threads
Layout
doubts over layouts
AWT FlowLayout to BorderLayout
From Mock (Layout)
Abhilash Quiz - Question on Layout
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:03:10.