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:
- A component can occupy more than one cell in the grid.
- The proportions between different rows and columns need not be equal.
- A component need not occupy or fill the complete cell.
- 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).
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
- void setConstraints(Component comp, GridBagConstraints con): Sets the component as per the GridBagConstraints, con.
- 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.
- Set the layout to the grid bag layout manager (creating an object and setting layout).
- Create an instance of GridBagConstraints.
- Set the constraints (properties) for each component separately.
- Inform the layout manager about the component and its constraints.
- 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
|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.
|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.
|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.|