• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Layout Policy

 
Ranch Hand
Posts: 40
  • 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 b extends Frame{

int x;
b(){
setLayout(new GridLayout());
add(new Panel());
add(new Button("One"));
add(new Button("Two"));
setLayout(new BorderLayout());
add(new Button("Three"), "North");
add(new Button("Four"), "South");
#line1 //setLayout( new FlowLayout());
add ( new Button("My Shining Button"));

}

public static void main(String[] arguments){
b fa=new b();
fa.setSize(400,300);
fa.setVisible(true);


}

}


The above source code gives frame with buttons at north, south and center.
But if you uncomment line #1 it gives 5 buttons in a row.
Can anyone pls explain why??
Thanks in adavance.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the last layout is what you get. The last layout you set was FlowLayout, which takes no positioning constraints. If you gave them earlier because BorderLayout was in effect, FlowLayout just ignores them and puts the components on First Come First Serve from Left to Right.
 
Avinash Rai
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy
But if that is the case then with the commented line also it should show all 5 buttons placed at North, South, East, West and Center.
But instead it shows only three with " My Shining Button" in the center.
If I comment out the line
add ( new Button("My Shining Button"));
Then it should show "button two" at center. But it is not the case.
Anyone any explaination??
Thanks in advance
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Avinash Rai:
Cindy
But if that is the case then with the commented line also it should show all 5 buttons placed at North, South, East, West and Center. But instead it shows only three with " My Shining Button" in the center.
If I comment out the line add ( new Button("My Shining Button"));
Then it should show "button two" at center. But it is not the case. Anyone any explaination?? Thanks in advance


Borderlayout allows only one component (which can be a container) added to each region. If no region is specified then "Center" is the default.
However, any component added to the screen before BorderLayout is set will be ignored by the BorderLayout manager.
 
Avinash Rai
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.
That means only BorderLayout ignores the components added before it was set. I tried with other layout mangers and they show all 5 buttons added before the layout manager was set.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Avinash,
Any layout that implements the LayoutManger interface ie GridLayout, FlowLayout, ViewportLayout, ScrollPaneLayout will use whatever components have been added to the container.
Layouts which implement the LayoutManager2 interface ie CardLayout, BorderLayout, GridBagLayout, BoxLayout will only display the components added since it was set.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Avinash Rai
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jane, Thanks a Lot.
This is new addition to my knowledge about LayoutManager interfaces. I also referred to API pages, there is a list of different LM interfaces.
Thanks again
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic