Learning Java
Custom Search

Previous Next

Grid Bag Layout

The GridBagLayout is similar to the GridLayout in that both use cells for arranging the components. However, with the GridBagLayout, each component can take up more than one cell; and within the cell, each component can be arranged differently. The placement, dimensions, alignment, etc for each component is defined by an additional class called the GridBagConstraints

In our example, we use the following fields from the GridBagConstraints class

  • fill = HORIZONTAL --- Resize the component horizontally but not vertically.
    fill = VERTICAL --- Resize the component vertically but not horizontally.
    fill = BOTH --- Resize the component both vertically and horizontally.
    fill = NONE --- Do not resize the component.
  • gridx --- Specifies the cell containing the leading edge of the component's display area, where the first cell in a row has gridx=0.
  • gridy --- Specifies the cell at the top of the component's display area, where the topmost cell has gridy=0.
  • gridwidth --- Specifies the number of cells in a row for the component's display area.
  • weightx --- Specifies how to distribute extra horizontal space.
  • weighty --- Specifies how to distribute extra vertical space.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimalGridBag extends JFrame {
    JButton dog = new JButton("Dog");
    JButton cat = new JButton("Cat");
    JButton bird = new JButton("Bird");
    JButton pig = new JButton("Pig");
    JButton mouse = new JButton("Mouse");
    JButton lion = new JButton("Lion");
    JButton tiger = new JButton("Tiger");
    JButton bear = new JButton("Bear");
    JButton deer = new JButton("Deer");

    public AnimalGridBag() {
        super("Animal GridLayout");
        setSize(260, 260);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel pane = new JPanel();
        GridBagLayout nature = new GridBagLayout();
	GridBagConstraints constr = new GridBagConstraints();
	constr.fill = GridBagConstraints.HORIZONTAL;

        pane.setLayout(nature);

        constr.gridx = 0;
        constr.gridy = 0;
        pane.add(dog, constr);

        constr.gridx = 1;
        constr.gridy = 0;
        pane.add(cat, constr);

        constr.gridx = 2;
        constr.gridy = 0;
        pane.add(bird, constr);

        constr.gridx = 0;
        constr.gridy = 1;
	constr.gridwidth = 2;
        pane.add(pig, constr);

        constr.gridx = 3;
        constr.gridy = 1;
        pane.add(mouse, constr);

        constr.gridx = 0;
        constr.gridy = 2;
	constr.gridwidth = 3;
        pane.add(lion, constr);

        constr.gridx = 1;
        constr.gridy = 4;
	constr.gridwidth = 1;
        pane.add(tiger, constr);

        constr.gridx = 2;
        constr.gridy = 5;
        constr.weightx = 10;
        constr.weighty = 10;
        pane.add(bear, constr);

        constr.gridx = 0;
        constr.gridy = 6;
	constr.gridwidth = 2;
        constr.weightx = 0;
        constr.weighty = 0;
        pane.add(deer, constr);

        add(pane);
        setVisible(true);
    }

    public static void main(String[] arguments) {
        AnimalGridBag frame = new AnimalGridBag();
    }
}

 

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved