Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes AWT 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 "AWT" Watch "AWT" New topic
Author

AWT

Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
The code below shows a blank frame instead of showing a single button named "Center" occupying the whole of the frame. Could anyone please explain?
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());
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

Thanx
Uvnik
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

This code adds all the comps to a flow layout.
then creates a new border layout. when you setVisible
to true, this acts on the border layout to which no
comps are added.
IMO, the other way may work okay ie; first border then
flow. I think border layout does something special.
If I am not wrong, this was discussed before. If no one
replies, you could try the search option...just suggesting.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
Thanx Satya. I could find a thread that discussed this issue. But am still not clear why doesn't it work only for BorderLayout. It works for all other layouts without changing the position of setVisible().
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186

Hi,

If you are using FlowLayout and GridLayout to set the layout then you can set it after adding components.
But , if you are using BorderLayout to set the layout then u must need to set it before adding the components.
thanks
sdev


"SCJP5 | SCWCD5| DEVELOPER"
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
I believe this has something to do with constraints. When you add components to a container which is using the LayoutManager2 interface i.e. BorderLayout, GridBagLayout, CardLayout, etc you have to explicitly specify the constraints object which will tell the layout manager where to position the componets inside the container. For eg:
add(button, BorderLayout.SOUTH);
However if you use FlowLayout or GridLayout which implement the LayoutManager interface, you do not have to set the constraints object. Infact you do not even have control over where the components will be placed. The LayoutManager takes care of that. I guess that's why it's behaving like this.
Have you tried setting it to GridLayout instead of BorderLayout? How does it behave then?
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
Hi Junaid,
I tried all (Grid, GridBag, Card, Flow and Border). But it does not work only with BorderLayout.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
My opinion is that this has to do with the default settings for the layout managers. When you create a LayoutManager without giving any constraints, the default constraints will be used. If you add a component to the BorderLayout without giving any constraints, for example, it will be added to the center pane, and componets added later will simply cover the ones added before so you only see the last one added. The following code:
import java.awt.*;
public class test 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 test()
{
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new BorderLayout());
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
test tf = new test();
}
}
is equivalent to:
import java.awt.*;
public class test 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 test()
{
setLayout(new BorderLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setSize(300,300);
setVisible(true);
validate();
}
public static void main(String args[])
{
test tf = new test();
}
}
and I guess the same should be ture for other layout managers
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
Hi Benli,
The point is that the two snippets posted by you are not equivalent Compile and see the difference.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Uvnik,
You're right. It works even for GridBagLayout & CardLayout. BorderLayout is the only exception. I wonder why
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AWT