Java Swing Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

java.awt.GridBagLayout - Grid Bag Layout

This section provides a tutorial example on how to create a GridBagLayout to layout components in a container. GridBagLayout can have many elements arranged in predefined rows and columns with non-equal sizes.

java.awt.GridBagLayout - A more complex layout that:

  • Divides the container into rows and columns. The number of rows and the number of columns are unlimited. Rows and columns are not equally divided.
  • Places components into the specified cells.
  • Uses the default size of each component.
  • Keeps component sizes unchanged when the container is resized.
  • Provides individual layout constraints for each component to control its layout behavior.

Once again, I wrote another program to try to display my window with GridBagLayout:

/**
 * GridBagLayoutTest.java
 * Copyright (c) 2002 by Dr. Herong Yang, http://www.herongyang.com/
 */
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest {
   public static void main(String[] a) {
      JFrame myFrame = new JFrame("GridBagLayout Test");
      myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      Container myPane = myFrame.getContentPane();
      myPane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.CENTER);
      myPane.add(getFieldPanel(),c);
      setMyConstraints(c,0,1,GridBagConstraints.CENTER);
      myPane.add(getButtonPanel(),c);
      myFrame.pack();
      myFrame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.EAST);
      p.add(new JLabel("Name:"),c);
      setMyConstraints(c,1,0,GridBagConstraints.WEST);
      p.add(new JTextField(16),c);
      setMyConstraints(c,0,1,GridBagConstraints.EAST);
      p.add(new JLabel("System:"),c);
      setMyConstraints(c,1,1,GridBagConstraints.WEST);
      p.add(getSystemPanel(),c);
      setMyConstraints(c,0,2,GridBagConstraints.EAST);
      p.add(new JLabel("Language:"),c);
      setMyConstraints(c,1,2,GridBagConstraints.WEST);
      p.add(getLanguagePanel(),c);
      setMyConstraints(c,0,3,GridBagConstraints.EAST);
      p.add(new JLabel("Year:"),c);
      setMyConstraints(c,1,3,GridBagConstraints.WEST);
      p.add(new JComboBox(new String[] {"2001","2002","2003"}),c);
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getSystemPanel() {
      JRadioButton unixButton = new JRadioButton("Unix",true);
      JRadioButton winButton = new JRadioButton("Window",false);
      ButtonGroup systemGroup = new ButtonGroup();
      systemGroup.add(unixButton);
      systemGroup.add(winButton);
      JPanel p = new JPanel(new GridBagLayout());
      p.add(unixButton);
      p.add(winButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("C++",true));
      p.add(new JCheckBox("Perl",false));
      return p;
   }
   private static void setMyConstraints(GridBagConstraints c, 
      int gridx, int gridy, int anchor) {
      c.gridx = gridx;
      c.gridy = gridy;
      c.anchor = anchor;
   }
}

Run it. What do you think about the result? Almost perfect, right?
Layout - GridBagLayout

All components are aligned correctly in both directions now. And all components are properly sized.

GridBagLayout seems to be good enough for my example.

Sample programs listed in this section have been tested with JDK 1.3.1, 1.4.1, 1.5.0, and 1.6.0.

Last update: 2009.

Sections in This Chapter

What Is Layout?

java.awt.BorderLayout - Border Layout

java.awt.FlowLayout - Flow Layout

java.awt.BoxLayout - Box Layout

java.awt.GridLayout - Grid Layout

java.awt.GridBagLayout - Grid Bag Layout

Dr. Herong Yang, updated in 2009
java.awt.GridBagLayout - Grid Bag Layout