aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Layout 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" Watch "Layout" New topic
Author

Layout

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Hi Friends,
Please read the following code and take time to explain me what's going on.



import java.awt.*;
public class TestFrame20 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 TestFrame20()
{
setLayout(new FlowLayout()); //1
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
validate();
setSize(300,300);
//setLayout(new GridLayout(3,3));
setLayout(new BorderLayout()); //2
setVisible(true);
}
public static void main(String args[])
{
TestFrame20 tf = new TestFrame20();
}
}


My question is if we interchange line marked 1 and line marked 2.I mean if i set the borderlayout at first and later i change that to flowlayout as the last statement the flowlayout is coming into picture. But the viceversa is not true. Why is that? If we change the layout to borderlayout after adding componenets it won't work???(Nothing is being displayed)
Please clarify this for me.
Thanks.
Bytheway i have set my exam date to 16'th May(finally) .But still scared though.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Sree,
I think the mistake you did was to change layout managers
before the component was visible. Sure, it added
those buttons fine to the flowlayout, however when you
changed the layout manager, the (dumb thing ) figured out
why should I redraw when the container is not visible?.

You validate() did not work for the same reason, I am guessing.
The AWT painting thread is checking isVisible() on your
container before proceeding to redraw the container.
See my code below, I have changed the order of some calls
in your code, and have removed the validate() method.
This works as you'd expect

<PRE>
public TestFrame20()
{
//setLayout(new FlowLayout()); //1

add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setSize(400,400);
setVisible(true);
//setLayout(new GridLayout(3,3));
setLayout(new BorderLayout()); //2

}
</PRE>
I am no AWT guru, but when I tried out a few combinations,
this is what I found out. I hope my explanation is correct. If not, I will eat my hat again :grin:
By the way, good luck for your exams. Don't worry you will done fine.

Ajith

[This message has been edited by Ajith Kallambella (edited May 05, 2000).]


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I dont quite get it when you say things do not work ? One thing which I think I should remind you, is that when you add components to a BorderLayout without specifying the locations (North, South, etc), the componets are added to the center of your frame. So if you do not have any other components in the other locations, I would expect your code to display the last button you added in your constructor, filling up the whole area.
Is this not right ?
bongadi
Greenhorn

Joined: Apr 17, 2000
Posts: 23
Hi!,
The addLayoutComponent() method signature of FlowLayout is ...
<code>
public void addLayoutComponent(String name, Component comp)
</code>
The addLayoutComponent() methd signature of BorderLayout is
<code>
public void addLayoutComponent(String name, object constraints )
</code>
when you call the <code>add()</code> of the container(Frame) then
the the appropriate <code> addLayoutComponent() </code> is
invoked. Also, the <code> addLayoutComponent() </code> is
invoked when the container (Frame, Panel etc.) is being
rendered.
FlowLayout manager's implementation of <code>addLayoutComponent()
</code> is a nop. Like so ; <code>addLayoutComponent() {}</code>
while the BorderLayout has an actual implementation.
When you are setting your layout to FlowLayout() the <code>
add(component) </code> adds and later <code> addLayoutComponent()
</code> renders it. But if your layout is BorderLayout() then
you are missing the constraints part. Hence the
component is not being rendered at all.
It does not matter what you set your layout to earlier, it is
the Layout manager which is being set before the rendering.
This is my understanding of Layout managers. I may be a bit
off. So please check it up.
HTH
BoN

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Ajith,
Did you try the code you posted. Is it working for you??? I still don't see anything.
Thanks
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Sree:
when I run the code, from Ajith, I see one button in the
whole screen of the frame named "center".
My reasoning is that the buttons are added to the default
border layout of the frame. Since no location is specified,
the button is added to the center of the screen. Infact all
the above buttons are added to the center of the frame, one
on top of the other.
Since the new Border layout is added after the setVisible
method, it is pretty much useless.
Note: If the setVisible method called after the new Border
Layout call, then I do not see anything on the screen. Just
one blank frame. In this case, the way I get it is that the
new layout is being displayed, to which no components have
been added. Since this layout is on top of the previous
default layout (which holds all the buttons), we donot
see anything.
Hope this helps.
Regds.
- satya
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Oops!! Ajith i am sorry. I guess i have interchanged setvisible() and setLayout() again. I gave setVisible() as the last statement.
Thanks a lot. But i am still not clear about one thing. If i set the layout to any other layout like grid or flow it's working fine.
ex:
setLayout(new GridLayout(3,3));
setVisible(true);
It's working fine and componenets are being displayed in a grid. Any clue??
Thanks every one for helping me.
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Sree,
Sorry for my late reply. Currently I am travelling and I don't have access to JDK. At this point, I can only say, Yes the code I have posted worked fine for me. Believe me it ran fine.
I would still stick to my argument that the components was not being redrawn just because the container was made invisible.
If I hear otherwise from my knowledgable Java friends, I will certainly post their viewpoints here.
Good luck.

Ajith
 
wood burning stoves
 
subject: Layout