• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Flow layout

 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}

Attemping to compile and run the above code

1. Will cause a compilation error - a Layout cannot be set after a component has been added with a preset Layout Manager.

2. Will cause a Runtime Exception - a Layout cannot be set after a component has been added with a preset Layout Manager.
3. Will compile cleanly and throw no runtime Exception. Only the button with label "Center" is visible and occupies the whole screen.
4. Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager.
5. Will compile and run cleanly, but no component is visible.
6. Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below
Answer is 5
i was thinking that answer should be 4.
please why it it 5?
vivek
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I think the answer is five because at first,u r setting the layout as flowlayout and adding buttons to it .after adding u r again setting the layout to borderlayout and u r not adding any buttons to it.so that's the reason u will get blank frame.
I may not correct,please check ur answer with others.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi greddy,
I am not saying that your reasoning is wrong but i have another question
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();
}
}
What will be the effect trying compile and run the above class?

1. Compilation error - a Layout cannot be set twice for a component.
2. Compilation error - One button is added without specifing the position in the borderLayout
3. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) "North","South","West","East" and "Center".
4. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) "South","West","East","North" and "Center".
5. No Compilation Error. The Buttons are arranged in the north , south, west, east and center regions, as in a borderlayout. Any further additions will follow the rules of FlowLayout manager.

And answer is 4.
This question is almost same except we have interchange the order of layout type.
Can some one please put light on it.
vivek
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please some one explain my quetions, i would really appreciate it.
regards
vivek
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek
What I understand from your 2 questions is there is a difference in behavior for BorderLayout comparing with FlowLayout and GridLayout ( Not in u r code, but I did verify that ).
First things first - I am not posting this to provide an answer but to ask this question again
Following is your code with my additions.
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()
{

1. //setLayout(new BorderLayout());
2. //setLayout(new GridLayout());
3. //setLayout(new FlowLayout());

add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);

4. //setLayout(new FlowLayout());
5. //setLayout(new BorderLayout());
6. //setLayout(new GridLayout());

validate();
setSize(300,300);
setVisible(true);
}
By running the code with different combinations like this what I found is only BorderLayout is NOT taking whatever is in the container ( component ) into the layout. All other combinations provide the default results as expected.
I am unable to find out enough explantion of this behavior from the books I have. Can anybody help me and Vivek with this ?
Thanks
ARS Kumar.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Kumar,
You are right something wrong with BorderLayout. i tried one more thing if u place any of lines (4,5,6) after setVisible(true); then none of them has any effect. button are displayed according to whatever we had set initially ( any of lines 1,2,3).
i don't know why?
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()
{
1. //setLayout(new BorderLayout());
2. //setLayout(new GridLayout());
3. //setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
4. //setLayout(new FlowLayout());
5. //setLayout(new BorderLayout());
6. //setLayout(new GridLayout());
validate();
setSize(300,300);
setVisible(true);
}

Can someone please help us.
regards
vivek
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know the answer but here is what i have found thus far.
Both FlowLayout and GridLayout implement the LayoutManager
interface. While the BorderLayout implements the LayoutManager2
interface.
copied form jdk 1.2 doc...
LayoutManager2
Defines an interface for classes that know how to layout Containers based on a layout constraints object. This interface extends the LayoutManager interface to deal with layouts explicitly in terms of constraint objects that specify how and where components should be added to the layout.
This minimal extension to LayoutManager is intended for tool providers who wish to the creation of constraint-based layouts. It does not yet provide full, general support for custom constraint-based layout managers.
Hope this helps,
Monty

[This message has been edited by monty6 (edited June 08, 2000).]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Further to Monty6's clue-
I think the BorderLayout calls the method 'invalidateLayout(Container target)' (availed from LayoutManager2 interface) when it is set to any container.
This method invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
hence the behaviour, (probably )
reply
    Bookmark Topic Watch Topic
  • New Topic