aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes LayoutManager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "LayoutManager" Watch "LayoutManager" New topic
Author

LayoutManager

Ricardo Polero
Ranch Hand

Joined: May 18, 2001
Posts: 128
Hi , everyone
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();
}
}
This code doesn't display any bottom , For me must display the las buttom in the middle of the frame (BorderLayout defaul!)
Someone can explaime WHY ?
Thanks in advance
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You added all the buttons when the LayoutManager was Flowlayout and therefore never told the LayoutManager WHERE to put the buttons.
For BorderLayout you HAVE TO set the location using
the syntax
add(bSouth, BorderLayout.SOUTH);
or else they all just Flow into the center.


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

Joined: Jan 09, 2001
Posts: 1492
Hi Ricardo,
The example shows you the difference between the interfaces LayoutManager and LayoutManager2. The FlowLayout which you originally use in the frame implements the LayoutManager interface. The BorderLayout which you set before display implements the LayoutManager2 interface. The LayoutManager2 needs constraints to set its children, therefore it during each add it supplies constraints to each component if none are supplied. However, your example has added all the buttons before setting the layout. What that means is that the buttons have no constraints. No constraints means no buttons! You must add the buttons again to get some constraints assigned to them (by you or the layout!).
This is the reason why most programmers would do well to set the layout first and then add the components!
Regards,
Manfred.
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Originally posted by Cindy Glass:
You added all the buttons when the LayoutManager was Flowlayout and therefore never told the LayoutManager WHERE to put the buttons.
For BorderLayout you HAVE TO set the location using
the syntax
add(bSouth, BorderLayout.SOUTH);
or else they all just Flow into the center.


Hi Cindy, just want one clarification. I had an impression that even if we don't specify the constraints in Border Layout, it adds the button or whatever in the CENTER, I may be wrong, it should have shown at least last button in the centre.
Regards
Jyotsna
Ricardo Polero
Ranch Hand

Joined: May 18, 2001
Posts: 128
Dear Manfred:
That was a good explanation, but I did many mock with similar approach and the answers was "the default location for each of them". This happen only with BorderLay ? (The defaul is Center)
Would you (or another expert) light me in this point ?
Thanks
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Jyotsna
Originally posted by Manfred Leonhardt:
The LayoutManager2 needs constraints to set its children

As Manfred said LayoutManager2 needs constraints to set it's children. If the CURRENT layout is BorderLayout and you do not specify a location, the BorderLayout class will use it's favorite default. The default behavior is handled by the add method. But Ricardo had FlowLayout in place at that time. FlowLayout did not set any favorite defaults at all, so when he switched over to BorderLayout (he is past the add method by now) there was not enough information for BorderLayout to work with.

[This message has been edited by Cindy Glass (edited June 26, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: LayoutManager
 
Similar Threads
doubts over layouts
java.awt BorderLayout and FlowLayout
Layout
Why the button doesn't appear in th frame?
Why the button doesn't appears in the Frame?