Using GridBagConstraints : GridBagConstraints « Swing « Java Tutorial






  1. GridBagConstraints specifies how to display a specific component.
  2. Every component added to a GridBagLayout container should have a GridBagConstraints object associated with it.
  3. Without GridBagConstraints, the GridBagLayout is a blank slate.
Using GridBagConstraints
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridBagButtons {
  private static final Insets insets = new Insets(0, 0, 0, 0);

  public static void main(final String args[]) {
    final JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    JButton button;
    // Row One - Three Buttons
    button = new JButton("One");
    addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Two");
    addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Three");
    addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    // Row Two - Two Buttons
    button = new JButton("Four");
    addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Five");
    addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    // Row Three - Two Buttons
    button = new JButton("Six");
    addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Seven");
    addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    frame.setSize(500, 200);
    frame.setVisible(true);
  }

  private static void addComponent(Container container, Component component, int gridx, int gridy,
      int gridwidth, int gridheight, int anchor, int fill) {
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
        anchor, fill, insets, 0, 0);
    container.add(component, gbc);
  }
}








14.95.GridBagConstraints
14.95.1.Using GridBagConstraintsUsing GridBagConstraints
14.95.2.Adding Components with a Relative X PositionAdding Components with a Relative X Position
14.95.3.Adding Components with a Relative Y PositionAdding Components with a Relative Y Position
14.95.4.Adding Components with Relative X and Y CoordinatesAdding Components with Relative X and Y Coordinates
14.95.5.Effects of the fill ConstraintEffects of the fill Constraint
14.95.6.Effects of the gridwidth ConstraintEffects of the gridwidth Constraint
14.95.7.Filling the Entire ColumnFilling the Entire Column
14.95.8.Using the REMAINDER Value for a WidthUsing the REMAINDER Value for a Width
14.95.9.Effects of the gridheight ConstraintEffects of the gridheight Constraint
14.95.10.A Simple Application That Uses GridBagConstraints.WESTA Simple Application That Uses GridBagConstraints.WEST
14.95.11.Use GridBagLayout to layout RadioButtonsUse GridBagLayout to layout RadioButtons
14.95.12.A GridBagLayout Example: weightx, weighty