• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Reset layout manager

Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.*;
import java.awt.event.*;
public class LayoutTest 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");

{bNorth.addActionListener(new ActionListener() {public void actionPerformed (ActionEvent ae) {System.exit(0);}}); }
public LayoutTest() {
super("Reset layout manager testing");


//reset layout manager
setLayout(new BorderLayout()); //compile and run, but present an empty frame.
//setLayout(new FlowLayout()); show all the five buttons in FlowLayout
//setLayout(new GridLayout()); show all the five buttons in GridLayout
//setLayout(new CardLayout()); just show bNorth button
//setLayout(new GridBagLayout()); show all the five butons in GridBagLayout
public static void main(String args[]){
LayoutTest t = new LayoutTest();

For the above program, when reset layout manager to BorderLayout, none of the five buttons will be shown. But reset to one of the other four layout managers in java.awt package will show all or some of the button(s).
Jane Griscti ever explained that: "BorderLayout implements LayoutManager2. All layouts implementing this interface keep track of their components; they do not query the container. In this example, no components were added while BorderLayout was active so it thinks it has none and therefore displays nothing."
But CardLayout and GridBagLayout also implements LayoutManager2, but after reset layout manager to either of them, some components have been displayed. I'm confused here. Can somebody explain?
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your observation just depends on the way the particular layout manager is implemented. Some of them work with the Container's list of components and some of them keep their own list.
To be safe - set the layout manager first.

author of:
    Bookmark Topic Watch Topic
  • New Topic