• 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

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");

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

add(bNorth,BorderLayout.NORTH);
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.WEST);
add(bEast,BorderLayout.EAST);
add(bCenter,BorderLayout.CENTER);

//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
setSize(300,300);
setVisible(true);
}
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?
Thanks.
 
Author and all-around good cowpoke
Posts: 13078
6
  • 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.
Bill

------------------
author of:
 
permaculture is giving a gift to your future self. After reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic