Java GridBagLayout

The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.

The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of the constraints object, we arrange the component's display area on the grid. The GridBagLayout manages each component's minimum and preferred sizes in order to determine the component's size. GridBagLayout components are also arranged in the rectangular grid but can have many different sizes and can occupy multiple rows or columns.

Constructor

GridBagLayout(): The parameterless constructor is used to create a grid bag layout manager.

GridBagLayout Fields

Modifier and TypeFieldDescription
double[]columnWeightsIt is used to hold the overrides to the column weights.
int[]columnWidthsIt is used to hold the overrides to the column minimum width.
protected Hashtable<Component,GridBagConstraints>comptableIt is used to maintains the association between a component and its gridbag constraints.
protected GridBagConstraintsdefaultConstraintsIt is used to hold a gridbag constraints instance containing the default values.
protected GridBagLayoutInfolayoutInfoIt is used to hold the layout information for the gridbag.
protected static intMAXGRIDSIZENo longer in use just for backward compatibility
protected static intMINSIZEIt is smallest grid that can be laid out by the grid bag layout.
protected static intPREFERREDSIZEIt is preferred grid size that can be laid out by the grid bag layout.
int[]rowHeightsIt is used to hold the overrides to the row minimum heights.
double[]rowWeightsIt is used to hold the overrides to the row weights.

GridBagLayout Methods

Modifier and TypeMethodDescription
voidaddLayoutComponent(Component comp, Object constraints)It adds specified component to the layout, using the specified constraints object.
voidaddLayoutComponent(String name, Component comp)It has no effect, since this layout manager does not use a per-component string.
protected voidadjustForGravity(GridBagConstraints constraints, Rectangle r)It adjusts the x, y, width, and height fields to the correct values depending on the constraint geometry and pads.
protected voidAdjustForGravity(GridBagConstraints constraints, Rectangle r)This method is for backwards compatibility only
protected voidarrangeGrid(Container parent)Lays out the grid.
protected voidArrangeGrid(Container parent)This method is obsolete and supplied for backwards compatibility
GridBagConstraintsgetConstraints(Component comp)It is for getting the constraints for the specified component.
floatgetLayoutAlignmentX(Container parent)It returns the alignment along the x axis.
floatgetLayoutAlignmentY(Container parent)It returns the alignment along the y axis.
int[][]getLayoutDimensions()It determines column widths and row heights for the layout grid.
protected GridBagLayoutInfogetLayoutInfo(Container parent, int sizeflag)This method is obsolete and supplied for backwards compatibility.
protected GridBagLayoutInfoGetLayoutInfo(Container parent, int sizeflag)This method is obsolete and supplied for backwards compatibility.
PointgetLayoutOrigin()It determines the origin of the layout area, in the graphics coordinate space of the target container.
double[][]getLayoutWeights()It determines the weights of the layout grid's columns and rows.
protected DimensiongetMinSize(Container parent, GridBagLayoutInfo info)It figures out the minimum size of the master based on the information from getLayoutInfo.
protected DimensionGetMinSize(Container parent, GridBagLayoutInfo info)This method is obsolete and supplied for backwards compatibility only

Example 1

FileName: GridBagLayoutExample.java

Output:

Java Gridbaglayout 1

Example 2

FileName: GridBagLayoutDemo.java

Output:

Java Gridbaglayout 2




Latest Courses