Swing Layouts

GridBag Layout

We will probably use the GridBag layout most often to layout complicated panels. Here is a list of what the GridBag layout does.

 

    *    We can specify which cell we want each component to go in and the component's position on the panel.

    *    If a component doesn't fill its allotted area, we can tell the GridBag how to position the component within the area.

    *    The rows and columns don't all have to be the same size. In addition, it automatically adjust the width of each column and the height of each row.

    *    We can create component that span multiple rows or columns.

    *    We can tell the GridBag to stretch a component to fill the entire space allotted to it.

For all the GridBag layout constructors and methods, look at the link below " Swing Method & Constructor Table."

Creating a GridBag Layout

JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());

With a panel that uses a GridBag layout, the add method accepts two parameters: the component to add and a GridBagConstraints object.

    GridBagConstraints:    It specifies where to place the component in the grid.

When using the GridBagConstraints, we call its constructor and then set any of the fields that we want.

 

/*
    This is a GridBagLyout Manager
    It demonstrate the use of GridBagConstraints
    JRadioButton, JCheckBox and JTextField

*
*/


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
 

public class GridBag extends JFrame
{
    JTextField nametxt, phonetxt, emailtxt;
    JLabel name, phone, email;
    JButton ok, exit;
    JRadioButton day, week, month, small, large;
    JCheckBox slow, fast, nextday;
    JPanel panel;

    public GridBag()
    {

        // Sets the title of the window


        setTitle("GridBag Layout Manager");

        // Sets the start location of the window

        setLocationRelativeTo(null);

        // Sets the action when the window is close

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create a panel and set the layout to GridBagLayout

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        // Create the labels


        name = new JLabel("Name");
        phone = new JLabel("Phone");
        email = new JLabel("E-Mail");

        // Adds the label to GridBagConstraints


        addItem(panel, name, 0, 0, 1, 1, GridBagConstraints.EAST);
        addItem(panel, phone, 0, 1, 1, 1, GridBagConstraints.EAST);
        addItem(panel, email, 0, 2, 1, 1, GridBagConstraints.EAST);

        // Create the text field


        nametxt = new JTextField(20);
        phonetxt = new JTextField(15);
        emailtxt = new JTextField(25);

        // Add the text field to the GridBagConstraints


        addItem(panel, nametxt, 1, 0, 2, 1, GridBagConstraints.WEST);
        addItem(panel, phonetxt, 1, 1, 1, 1, GridBagConstraints.WEST);
        addItem(panel, emailtxt, 1, 2, 2, 1, GridBagConstraints.WEST);

        // Creates a vertical box to organize the buttons

        Box timebox = Box.createVerticalBox();

        // Creates the radio buttons


        day = new JRadioButton("Day");
        week = new JRadioButton("Week");
        month = new JRadioButton("Month");

        // Group the buttons together

        ButtonGroup timegroup = new ButtonGroup();

        timegroup.add(day);
        timegroup.add(week);
        timegroup.add(month);

        // Add the buttons to the Box

        timebox.add(day);
        timebox.add(week);
        timebox.add(month);

        // Sets the bevelBroder title


        timebox.setBorder(BorderFactory.createTitledBorder("Time Frame"));

        // Adds the box into the GridBagConstraints


        addItem(panel, timebox, 0, 3, 1, 1, GridBagConstraints.NORTH);

        // Create a new box to handles the size


        Box sizebox = Box.createVerticalBox();

        // Create the radio buttons


        small = new JRadioButton("Small");
        large = new JRadioButton("Large");

        // Group the buttons together


        ButtonGroup sizegroup = new ButtonGroup();

        sizegroup.add(small);
        sizegroup.add(large);

        // Add the buttons to the box


        sizebox.add(small);
        sizebox.add(large);

        // Create a bevelBorder title

        sizebox.setBorder(BorderFactory.createTitledBorder("Size"));

        // Adds to the GridBagConstraints

        addItem(panel, sizebox, 1, 3, 1, 1, GridBagConstraints.NORTH);

        // Creates another box layout

   
        Box speed =Box.createVerticalBox();

        // Create JCheckBox buttons

        slow = new JCheckBox("Slow");
        fast = new JCheckBox("Fast");
        nextday = new JCheckBox("Next Day");

        // Group the buttons together

        ButtonGroup speedgroup = new ButtonGroup();

        speedgroup.add(slow);
        speedgroup.add(fast);
        speedgroup.add(nextday);

        // Add the buttons to the box


        speed.add(slow);
        speed.add(fast);
        speed.add(nextday);

        // Create bevelborder title


        speed.setBorder(BorderFactory.createTitledBorder("Speed"));

        // Adds to the GridBagConstraints

        addItem(panel, speed, 2, 3, 1, 1, GridBagConstraints.NORTH);

        // Create the buttons

        ok = new JButton("OK");
        exit = new JButton("EXIT");

        // Create a horizontal box to hold the buttons


        Box buttonbox = Box.createHorizontalBox();

        // Add the ok button to the box


        buttonbox.add(ok);

        // Create a space between ok and exit button


        buttonbox.add(Box.createHorizontalStrut(50));

        // Add the exit button to the box


        buttonbox.add(exit);

        // Add the button tp the GridBagConstraints

        addItem(panel, buttonbox, 2, 4, 1, 1, GridBagConstraints.NORTH);

        // Add the panel to the content pane

        add(panel);

        // Pack everything up


        pack();

        // Sets the window to be visible


        setVisible(true);

    }

  
 /*
        Here is the void method of addItem
        It sets the height, width, location, and align of the components

    *
    */


    public void addItem(JPanel p, JComponent c, int x, int y, int wid, int hei, int align)
    {

        // Creates a GridBagConstraints


        GridBagConstraints grid = new GridBagConstraints();

        // Sets the position of the components


        grid.gridx = x;
        grid.gridy = y;
   

        // Sets the number of columns & rows span by the components


        grid.gridwidth = wid;
        grid.gridheight = hei;

        // Tells the grid layout how to apportion space

        grid.weightx = 100.00;
        grid.weighty = 100.00;

        // It indicates how much space to use as padding around each component


        grid.insets = new Insets(5,5,5,5);

        // Indicates where to place the component if it doesn't fill the space


        grid.anchor = align;

        // Indicates whether to stretch the object to fill available space or not


        grid.fill = GridBagConstraints.NONE;

        // adds to the panel

        p.add(c,grid);

    }

    public static void main(String[] args)
    {
        new GridBag();

    }
}


ClickHere to download GridBag.java

 

 

 

  <Previous><Java 24><Home><Swing Method & Constructor Table>