Java AWT GridBagLayout Manager

This is the king of all layout managers because it can handle almost any type of layout arrangement. It is more flexible and at the same time more complex to use. This is an extension of GridLayout manager. Both grid layout and grid bag layout use the concept of grid of cells (spread between rows and columns). This layout manager is used when the components are dissimilar in size as in the case of a Calculator buttons design. In GridLayout, one component occupies only one cell but in GridBagLayout, one component can occupy more than one cell either horizontally or vertically or both.

Comparison of GridLayout with GridBagLayout

A grid bag layout differs from grid layout in the following:

  1. A component can occupy more than one cell in the grid.
  2. The proportions between different rows and columns need not be equal.
  3. A component need not occupy or fill the complete cell.
  4. Component inside the cell can be arranged in different ways (this possibility arises when the component does not occupy the complete space of the cell).

class GridBagLayout

GridBagLayout manager is represented by a class called GridBagLayout from java.awt package.

Following is the class signature

public class GridBagLayout extends Object implements LayoutManager2, Serializable

Following are some important methods

  1. void setConstraints(Component comp, GridBagConstraints con): Sets the component as per the GridBagConstraints, con.
  2. GridBagConstraints getConstraints(Component comp): Returns all the properties of the component as an object of GridBagConstraints.


It is a helper class used in combination with GridBagLayout object. This class sets the properties to a component. GridBagLayout manager lays the components in a container as per the properties set by the GridBagConstraints object. GridBagConstraints class includes methods and variables to set the properties of a component – its placement, dimension, alignment etc. The ultimate size and position of a component depends on the relationship between the grid bag and the constraints.

Following is the class signature

public class GridBagConstraints extends Object implements Cloneable,

Steps of using GridBagLayout

To simplify the procedure of using GridBagLayout manager, it is better to formulate some steps, like you followed for Menus.

  1. Set the layout to the grid bag layout manager (creating an object and setting layout).
  2. Create an instance of GridBagConstraints.
  3. Set the constraints (properties) for each component separately.
  4. Inform the layout manager about the component and its constraints.
  5. Add the component to the container.

Fields of GridBagConstraints

Always the component to be added to the container is associated with GridBagConstraints object which dictates the properties of the component in terms of position and size. The GridBagConstraints includes a big family of public fields (variables) that sets the constraints (properties) to a component.

Following are some important fields


Variable Description
gridx, gridy These variables are used to specify the location where the component is to be placed. For example, the top-left cell location is specified as gridx = 0 and gridy = 0. If no values for these variables are specified, the layout manager assumes them to be as GridBagConstraints.RELATIVE which means the component is to be placed next to the existing one (it is relative placement: either to the right or below the existing one).
gridwidth, gridheight These variables are used to give the size of the component in terms of cells. The default value is 1, if no value is specified. Assigning GridBagConstraints.REMAINDER to these variables means, the component getting added is the last one in its row.
fill This field is used when the component’s display area is larger than the component’s size. It tells whether to resize the component or not when the frame is resized. The value can be one of the following.

  1. GridBagConstraints.HORIZONTAL: Component grows horizontally
  2. GridBagConstraints.VERTICAL: Component grows vertically
  3. GridBagConstraints.BOTH: Component grows both ways
  4. GridBagConstraints.NONE: Component does not grow at all
anchor This variable specifies the placement (or location) of the component within the cell when the component does not fill the whole cell. The value can by any one of the following.

  1. GridBagConstraints.NORTH
  2. GridBagConstraints.NORTHEAST
  3. GridBagConstraints.EAST
  4. GridBagConstraints.SOUTHEAST
  5. GridBagConstraints.SOUTHWEST
  6. GridBagConstraints.WEST
  7. GridBagConstraints.NORTHWEST
  8. GridBagConstraints.CENTER (the default one)
ipadx, ipady ipadx pixels are added to the left and right of the minimum size of the component and ipady pixels are added to the top and bottom.
insets specifies the empty space between the container border and the components.
weightx, weighty distribute the space when the container expands. The GridBagLayout distributes the space between the columns horizontally or vertically as per the weights of weightx and weighty. The columns or cells with more weight get more space. The cells with 0 weight value will not get any extra space. The range of values must be 0.0 to 1.0. The default value of these fields are 0 and when specified it must be a non-negative value. The actual behavior is also dependent on the fill factor.

Leave a Comment

Your email address will not be published.