Win a copy of Fixing your Scrum this week in the Agile forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Layout Managers

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please compile and run the following code.
It creates a frame window and changes its layout manager every 10 seconds. If you run the code you will see that when layout changes from Flow to Border,both are visible but when layout is changed to Grid it hides previous layouts and arrange ALL components in 2,1 Grid.Why?
import java.awt.*;
class BLTest extends Frame{
public static void main(String args[]){
Frame f=new BLTest();
}
BLTest(){
setVisible(true);
setLayout(new FlowLayout());
add(new Button("First"));
add(new Button("Second"));
pack();
try{
Thread.sleep(10000);
}catch(InterruptedException e){}
setLayout(new BorderLayout());
add(new Button("North"),"North");
add(new Button("East"),"East");
add(new Button("West"),"West");
add(new Button("Center"),"Center");
add(new Button("South"),"South");
pack();
try{
Thread.sleep(10000);
}catch(InterruptedException e){}
setLayout(new GridLayout(2,1));
for(int i=0;i<5;i++)
add(new Button(""+i));
pack();
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you added the 2 buttons to the FlowLayoutManager they flowed just fine. They were still there when you switched to Border, but you didn't tell the LayoutManager where to put them, so they float there. Grid Layout managers insist that EVERY component in the container be in a Grid area. The other components were there first, they got put into the grid first, then you added the other buttons so they grid expanded to try to accommodate all of the components.
 
Ashish Agarwal
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot Cindy you cleared my doubt.
Ashish

Originally posted by Cindy Glass:
When you added the 2 buttons to the FlowLayoutManager they flowed just fine. They were still there when you switched to Border, but you didn't tell the LayoutManager where to put them, so they float there. Grid Layout managers insist that EVERY component in the container be in a Grid area. The other components were there first, they got put into the grid first, then you added the other buttons so they grid expanded to try to accommodate all of the components.


 
reply
    Bookmark Topic Watch Topic
  • New Topic