5.3.3.1 Using GridBagLayout


In the GridBagLayout a container is divided into a specified number of rows and columns, but components may occupy more than one cell of the grid (their "display areas"). Heights of rows and widths of columns are not necessarily the same in a GridBagLayout. All this gives you a lot control on the size and position of components, on filling of unused space, on internal and external padding, and so on.

With each component in a GridBagLayout is associated a set of constraints contained within a GridBagConstraints instance that specifies how the component is to be laid out. GridBagLayout is rather complicated to use. Instead of listing all possibilities of GridBagLayout we shall only give one example to serve as a prototype.

The Applet
The Java Code
import java.applet.Applet;
import java.awt.*;

public class FontSelector extends Applet {

  private Font currentFont, defaultFont;

  public void init() {
    //  get default font
    defaultFont = getFont();
    currentFont = defaultFont;
    //  specify the gridbag layout
    GridBagLayout grid = new GridBagLayout();
    int rowHeights[] = {0,30,30,30,30};
    int columnWidths[] = {0,30,30,30};
    double rowWeights[] = {0.0, 0.0, 0.0, 0.0, 0.0};
    double columnWeights[] = {0.0, 0.0, 0.0, 0.0};
    grid.rowHeights = rowHeights;
    grid.columnWidths = columnWidths;
    grid.rowWeights = rowWeights;
    grid.columnWeights = columnWeights;
    //  create the components
    Label nameLabel = new Label("Java Font Name:");
    nameLabel.setFont(new Font("Helvetica", Font.BOLD, 14));
    add(nameLabel);
    //
    Label sizeLabel = new Label("Font Size:");
    sizeLabel.setFont(new Font("Helvetica", Font.BOLD, 14));
    add(sizeLabel);
    //
    Label attributesLabel = new Label("Attributes:");
    attributesLabel.setFont(new Font("Helvetica", Font.BOLD, 14));
    add(attributesLabel);
    //
    Choice nameChoice = new Choice();
    nameChoice.addItem("default");
    nameChoice.addItem("Courier");
    nameChoice.addItem("Dialog");
    nameChoice.addItem("DialogInput");
    nameChoice.addItem("Helvetica");
    nameChoice.addItem("Symbol");
    nameChoice.addItem("TimesRoman");
    nameChoice.addItem("ZapfDingbats");
    nameChoice.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(nameChoice);
    //     
    Choice sizeChoice = new Choice();
    sizeChoice.addItem("* ");
    sizeChoice.addItem("10");
    sizeChoice.addItem("12");
    sizeChoice.addItem("14");
    sizeChoice.addItem("20");
    sizeChoice.addItem("24");
    sizeChoice.addItem("36");
    sizeChoice.setFont(new Font("Dialog", Font.PLAIN, 12));
    add(sizeChoice);
    //
    Panel stylePanel = new Panel();
    stylePanel.setLayout(new GridLayout(4, 1));
    CheckboxGroup style = new CheckboxGroup();
    //  specify font attributes
    Checkbox plain = new Checkbox("Plain", style, true);
    plain.setFont(new Font("Dialog", Font.PLAIN, 12));
    Checkbox bold = new Checkbox("Bold", style, false);
    bold.setFont(new Font("Dialog", Font.BOLD, 12));
    Checkbox italic = new Checkbox("Italic", style, false);
    italic.setFont(new Font("Dialog", Font.ITALIC, 12));
    Checkbox bolditalic = new Checkbox("Bold Italic", style, false);
    bolditalic.setFont(new Font("Dialog", Font.BOLD + Font.ITALIC, 12));
    //  add style panel
    stylePanel.add(plain);
    stylePanel.add(bold);
    stylePanel.add(italic);
    stylePanel.add(bolditalic);
    add(stylePanel);
    //
    Label sampleLabel = new Label("Sample:");
    sampleLabel.setFont(new Font("Helvetica", Font.BOLD, 14));
    add(sampleLabel);
    //
    TextArea sampleArea = new TextArea(
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789",
      3, 27);
    sampleArea.setFont(defaultFont);
    add(sampleArea);
    //
    // geometry management
    //
    GridBagConstraints constraints = new GridBagConstraints();
    //  row 1, column 1: font name label
    reset(constraints);
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.anchor = GridBagConstraints.SOUTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(nameLabel, constraints);
    //  row 1, column 2: font size label
    reset(constraints);
    constraints.gridx = 2;
    constraints.gridy = 1;
    constraints.anchor = GridBagConstraints.SOUTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(sizeLabel, constraints);
    //  row 1, column 3: font attributes
    reset(constraints);
    constraints.gridx = 3;
    constraints.gridy = 1;
    constraints.anchor = GridBagConstraints.SOUTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(attributesLabel, constraints);
    //  row 2, column 1: logical font name
    reset(constraints);
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(nameChoice, constraints);
    //  row 2, column 2: font size
    reset(constraints);
    constraints.gridx = 2;
    constraints.gridy = 2;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(sizeChoice, constraints);
    //  row 2, column 3: Checkbox group of font attributes
    reset(constraints);
    constraints.gridx = 3;
    constraints.gridy = 2;
    constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.fill = GridBagConstraints.BOTH;
    grid.setConstraints(stylePanel, constraints);
    //  row 3, all columns: sample label
    reset(constraints);
    constraints.gridx = 1;
    constraints.gridy = 3;
    constraints.gridwidth = 3;
    constraints.anchor = GridBagConstraints.SOUTHWEST;
    constraints.fill = GridBagConstraints.NONE;
    grid.setConstraints(sampleLabel, constraints);
    //  row 4, all columns: sample area
    reset(constraints);
    constraints.gridx = 1;
    constraints.gridy = 4;
    constraints.gridwidth = 3;
    constraints.anchor = GridBagConstraints.SOUTHWEST;
    constraints.fill = GridBagConstraints.BOTH;
    grid.setConstraints(sampleArea, constraints);
    //  set gridbag layout
    setLayout(grid);
    }

private void reset(GridBagConstraints con) {
    //  default constraints
    con.gridx = GridBagConstraints.RELATIVE;
    con.gridy = GridBagConstraints.RELATIVE;
    con.gridwidth = 1;
    con.gridheight = 1;
    con.weightx = 0;
    con.weighty = 0;
    con.anchor = GridBagConstraints.CENTER;
    con.fill = GridBagConstraints.NONE;
    con.insets = new Insets(0, 0, 0, 0);
    con.ipadx = 0;
    con.ipady = 0;
  }

}