Copyright ©1996-1998 by Axel T. Schreiner.  All Rights Reserved.



Experiments with GridBagLayout -- layout/Grid.java

[ Applet]

Grid illustrates how changes in the GridBagConstraints influence buttons. The GridBagConstraints of the current button can be changed in an inspector window with CardLayout; the effects are visible in the grid window.

A name can be entered in the middle of the top row of the inspector window. If it is not yet known, a new button is created in the grid window. At the top right is a Choice with the names of all buttons. There, by typing in the center, or by pressing the button in the grid window itself an existing button can be selected. At the top left is a button to erase the current button in the grid window.



Main program

As an Applet the inspector should be on the web page, i.e., it should be a Panel. The grid uses a Frame known to Grid so that Grid may change the size if necessary.

As Grid creates the first Card and the first button, the Cards need to be picked up later and entered into the inspector panel.
{apps/layout/Grid.java}
import java.awt.*;
import java.awt.event.*;

/** A class to experiment with GridBagLayout */
public class Grid extends Panel {
protected Frame inFrame; protected Cards cards;
public Grid (Frame inFrame) {
super(new GridBagLayout());
this.inFrame = inFrame;
cards = new Cards("first", new ConstraintsCard());
}
public Cards getCards () {
return cards;
}
public static void main (String args []) {
Frame cardsFrame = new Frame("GridBagConstraints Inspector"),
gridFrame = new Frame("Grid");

Grid grid = new Grid(gridFrame);
gridFrame.add("Center", grid);

cardsFrame.add("Center", grid.getCards());
cardsFrame.pack(); cardsFrame.show();

Dimension d = cardsFrame.getSize();
gridFrame.pack();
Rectangle r = gridFrame.getBounds();
r.x += d.width; r.y += d.height;
gridFrame.setBounds(r); gridFrame.show();

WindowListener wl = new WindowAdapter() {
public void windowClosing (WindowEvent e) {
System.exit(0);
}
};
cardsFrame.addWindowListener(wl); gridFrame.addWindowListener(wl);
}
{}
The Grid should appear up to the right and below the inspector.



ConstraintsCard

ConstraintsCard connects a ConstraintsInspector with Cards and a button in Grid. If necessary, Grid is allowed to grow at each change, but it may not reduce it's own size.
{apps/layout/Grid.java}
protected class ConstraintsCard extends ConstraintsInspector
implements Cards.Card {
protected Button button;
public ConstraintsCard () {
super(new GridBagConstraints());
addComponentListener(new ComponentAdapter() {
public void componentShown (ComponentEvent e) {
button.setBackground(Color.red);
}
public void componentHidden (ComponentEvent e) {
button.setBackground(Color.yellow);
}
});
}
public Cards.Card newCard () {
return new ConstraintsCard();
}
public void actionPerformed (ActionEvent e) {
if (e.getActionCommand().equals("add")) {
Grid.this.add(button = new Button(e.getSource().toString()), gbc);
button.addActionListener(new Fix());
changed("add", 0);
} else if (e.getActionCommand().equals("delete")) {
Grid.this.remove(button);
changed("delete", 0);
}
}
protected void changed (String nm, int v) {
Rectangle r = inFrame.getBounds();
((GridBagLayout)Grid.this.getLayout()).setConstraints(button, gbc);
Grid.this.invalidate();
inFrame.pack();
Dimension d = inFrame.getPreferredSize();
if (r.width < d.width) r.width = d.width;
if (r.height < d.height) r.height = d.height;
inFrame.setBounds(r);
}
}
private class Fix implements ActionListener { // avoid IllegalAccessError
public void actionPerformed (ActionEvent ae) {
cards.showCard(ae.getActionCommand());
}
}
}
{}
It is not clear why Fix is required rather than an anonymous ActionListener for the button. The application worked without Fix, but every browser only worked with the member class.

29/Apr/1998