Class GridBagConstraints in Java:

The GridBagConstraints class specifies constraints for components that are laid out using GridBagLayout class. This class encapsulates the instance variables to guide GridBagLayout on how to position a given component within the Container.

In short, The GridBagConstraints class provides the means to control the layout of components within a Container whose LayoutManager is GridBagLayout.

The GridBagLayout is written here for reference.

The class structure of class GridBagConstraints is given as:

public class java.awt.GridBagConstraints extends java.lang.Object implements java.lang.Cloneable, java.io.Serializable{
//member elements
public int anchor;
//This field is used when the component is smaller than its display area.It determines where, within the area, to place the component.
//Valid values are as follows:
public final static int CENTER;
public final static int EAST;
public final static int NORTH;
public final static int NORTHEAST;
public final static int NORTHWEST;
public final static int SOUTH;
public final static int SOUTHWEST;
public final static int SOUTHEAST;
public final static int WEST;
//The default value of GridBagConstraints is CENTER.
public int fill;
//The fill field has one of the following value:
public final static int BOTH;
public final static int HORIZONTAL;
public final static int NONE;
public final static int VERTICAL;
//The filed is used when the component's display area is larger than component's requested size.It determines whether to resize the component and if so valid values are as follows:
GridBagConstraints.NONE;//do does not resize the component.
GridBagConstraints.HORIZONTAL;//Make the component wide enough to fill its display area horizontally but do not change its height.
GridBagConstraints.VERTICAL;//Make the component lengthy enough to fill its display area vertically but do not change its width.
GridBagConstraints.BOTH;//Make component fill its display area entierly. The default value is GridBagConstraints.NONE;
public int gridHeight;//specifies the number of cells in a column for the component's display area. Use GridBagConstaints.REMAINDER to specify the component to be the last one in its column.Use GridBagConstaints.RELATIVE to specify that the component is the next to last one it's a column. The default is 1.
public int gridWidth;//specifies the number of cells in a row for the component's display area. Use GridBagConstaints.REMAINDER to specify that the component is the last one in its row.Use GridBagConstaints.RELATIVE to specify that the component is the next to the last one in its row. The default is 1.
public int gridx;//Specifies the cell at the left of the component's display area, where the leftmost cell has gridx=0.The value GridBagConstaints.RELATIVE specifies that the component to be placed just to the right of the component that was added to the container just before this component was added. The default value is GridBagConstaints.RELATIVE.
public int gridy; //Specifies the cell at the top of the component's display area, where the topmost cell has gridy=0. The value GridBagConstaints.RELATIVE specifies that the component to be placed just below the component that was added to the container just before this component was added. The default value is GridBagConstaints.RELATIVE.
//The possible values for gridHeight,gridWidth,gridx,gridy:
public final static int REMAINDER;
public final static int RELATIVE;
public Insets insets;//this field specifies the extended padding of the component, the minimum amount of space between the component and the edge of its the display area. the default value is new Insets(0,0,0,0);
public int ipadx; //This field specifies the internal padding that is how much space to add to the minimum size of the component. The width of the component is at least it's minimum width plus ipadx*2pixels. The default value is 0.
public int ipady; //This field specifies the internal padding that is how much space to add to the minimum size of the component. The height of the component is at least it's minimum height plus ipady*2pixels. The default value is 0.
public double weightx; //this field specifies how to distribute extra vertical space. The GridBagLayout manager calculates the weight of a row to be the maximum weightx of all the components in a row. If the resulting layout is smaller vertically than the area it needs to fill, the extra space is distributed to each row in proportion to its weight. A row that has wight 0 receives no extra space. If all the weights are zero, all the extra space appears between the grids of the cell and the top and bottom edge. The default value is zero(0).
public double weighty; //this field specifies how to distribute extra horizontal space. The GridBagLayout manager calculates the weight of a column to be the maximum weighty of all the components in a row. If the resulting layout is smaller horizontally than the area it needs to fill, the extra space is distributed to each column in proportion to its weight. A column that has weight 0 receives no extra space. If all the weights are zero, all the extra space
appears between the grids of the cell and the left and right edge. The default value is zero(0).instance variable
//constructor
public GridBagConstaints();
//creates a GridBagConstaints object with all the fields set to their default value.
//method
public Object clone();
//returns a copy of this GridBagConstaints.
}

The details of the class structure are given as follows:

public final static int BOTH;

public final static int BOTH represents a constant for possible fill value.

public final static int CENTER;

public final static int CENTER represents a constant for possible anchor value.

public final static int EAST;

public final static int EAST represents a constant for possible anchor value.

public final static int HORIZONTAL;

public final static int HORIZONTAL represents a constant for possible fill value.

public final static int NONE;

public final static int NONE represents a constant for possible fill value.

public final static int NORTH;

public final static int NORTH represents a constant for possible anchor value.

public final static int NORTHEAST;

public final static int NORTHEAST represents a constant for possible anchor value.

public final static int NORTHWEST;

public final static int NORTHWEST represents a constant for possible anchor value.

public final static int RELATIVE;

public final static int RELATIVE represents a constant for possible gridx, gridy, gridwidth, or gridheight value.

public final static int REMAINDER;

public final static int REMAINDER represents a constant for possible gridwidth or gridheight value.

public final static int SOUTH;

public final static int SOUTH represents a constant for possible anchor value.

public final static int SOUTHEAST;

public final static int SOUTHEAST represents a constant for possible anchor value.

public final static int SOUTHWEST;

public final static int SOUTHWEST represents a constant for possible anchor value.

public final static int VERTICAL;

public final static int VERTICAL represents a constant for possible fill value.

public final static int WEST;

public final static int WEST represents a for possible anchor value.

public int anchor;

public int anchor represents the alignment of the component in the event that it is smaller than the space allotted for it by the layout manager; e.g., CENTER centers the object within the region.

public int fill;

public int fill represents the component’s resize policy if additional space available.

public int gridheight;

public int gridheight represents the number of columns a component occupies.

public int gridwidth;

public int gridwidth represents the number of rows a component occupies.

public int gridx;

public int gridx represents the horizontal grid position at which to add the component.

public int gridy;

public int gridy represents the vertical grid position at which to add the component.

public Insets insets;

public Insets insets represent the outer padding around the component.

public int ipadx;

public int ipadx represents the internal padding within the component in both the right and left directions.

public int ipady;

public int ipady represents the internal padding within the component in both the top and bottom directions.

public double weightx

public double weight represents the percentage of extra horizontal space that will be given to this component if there is additional space available within the container.

public double weighty

public double weight represents the percentage of extra vertical space that will be given to this component if there is additional space available within the container.

public GridBagConstraints();

public GridBagConstraints() constructs a GridBagConstraints object.

public Object clone();

public Object clone() method returns a new instance of GridBagConstraints with the same values for constraints.

This method returns a new instance of GridBagConstraints with the same values for constraints.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment