• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Layout Managers

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.


 
BWA HA HA HA HA HA HA! Tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!