# What is the importance of the GridBagConstraints class in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

A GridBagLayout is a very flexible layout manager that allows us to position the components relative to one another using constraints. Each GridBagLayout uses a dynamic rectangular grid of cells with each component occupying one or more cells called its display area. Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that specifies how the component is laid out within its display area.

## GridBagConstraints

We can customize a GridBagConstraints object by setting one or more of its public instance variables. These variables specify the component location, size, growth factor, anchor, inset, filling, and padding.

• gridx: An int value that specifies the leftmost cell that the component occupies. gridx specifies the column in which the component will be placed.
• gridy: An int value that specifies the topmost cell that the component occupies. gridy specifies the row in which it will be placed.
• gridheight: An int value that specifies the number of vertical cells that a component occupies.
• gridwidth: An int value that specifies the number of horizontal cells that a component occupies.
• ipadx: An int value that specifies the amount of internal horizontal padding to be added to each control.
• ipady: An int value that specifies the amount of internal vertical padding to be added to each control.
• insets: An Insets object that specifies the amount of empty space to leave on each side of the cell.
• weightx: A double value that specifies how extra horizontal space is distributed if the resulting layout is horizontally smaller than the area allotted.
• weighty: A double value that specifies how extra vertical space is distributed if the resulting layout is vertically smaller than the area allotted.
• anchor: An int value that specifies the alignment of a component within a cell.
• fill: An int value that specifies what to do with extra space in a cell.
• RELATIVE: For the gridx and gridy fields, this field specifies that the component will be placed next to the last added component. For the gridwidth and gridheight fields, this field specifies that the component will be the next-to-last component in a row or column.
• REMAINDER: For the gridwidth and gridheight fields, this field specifies that a component is the last component in a row or column.

## Example

import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest {
public static void main(String[] a) {
JFrame frame = new JFrame("GridBagLayout Test");
Container myPane = frame.getContentPane();
myPane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
setMyConstraints(c,0,0,GridBagConstraints.CENTER);
setMyConstraints(c,0,1,GridBagConstraints.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 250);
frame.setLocationRelativeTo(null);
frame.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);
setMyConstraints(c,1,0,GridBagConstraints.WEST);
setMyConstraints(c,0,1,GridBagConstraints.EAST);
setMyConstraints(c,1,1,GridBagConstraints.WEST);
setMyConstraints(c,0,2,GridBagConstraints.EAST);
setMyConstraints(c,1,2,GridBagConstraints.WEST);
setMyConstraints(c,0,3,GridBagConstraints.EAST);
setMyConstraints(c,1,3,GridBagConstraints.WEST);
return p;
}
private static JPanel getButtonPanel() {
JPanel p = new JPanel(new GridBagLayout());
return p;
}
private static JPanel getSystemPanel() {
ButtonGroup systemGroup = new ButtonGroup();
JPanel p = new JPanel(new GridBagLayout());
return p;
}
private static JPanel getLanguagePanel() {
JPanel p = new JPanel(new GridBagLayout());
return p;
}
private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
c.gridx = gridx;
c.gridy = gridy;
c.anchor = anchor;
}
}

## Output

Published on 10-Jul-2019 13:01:09