It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Urgent help please on AWT - mixing layout managers 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 "Urgent help please on AWT - mixing layout managers" Watch "Urgent help please on AWT - mixing layout managers" New topic

Urgent help please on AWT - mixing layout managers

R Arun
Ranch Hand

Joined: Apr 20, 2002
Posts: 33
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());
setLayout(new FlowLayout());
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 ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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 you can download the first edition of Thinking in Java.
Richard G Balwin has written a lot here

SCJP2. Please Indent your code using UBB Code
R Arun
Ranch Hand

Joined: Apr 20, 2002
Posts: 33
Thanks for the quick response Jose.
Eduard Manas
Ranch Hand

Joined: May 12, 2002
Posts: 69
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.

I agree. Here's the link:
subject: Urgent help please on AWT - mixing layout managers
It's not a secret anymore!