Java, being a versatile programming language, provides developers with numerous tools and techniques to create visually appealing and well-organized user interfaces (UIs). One such tool is the GridLayout, which offers a systematic approach to arrange components within a container in rows and columns. In this section, we will delve into the intricacies of GridLayout in Java, exploring its functionalities, advantages, and implementation techniques.
Grid Layout is a part of the Java AWT (Abstract Window Toolkit) package, designed to simplify the process of arranging UI components in a grid-like fashion. Unlike other layout managers such as FlowLayout or BorderLayout, Grid Layout divides the container into a fixed number of rows and columns, allowing components to be placed at specific intersections.
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.
GridBagLayout(): The parameterless constructor is used to create a grid bag layout manager.
| Modifier and Type | Field | Description |
|---|---|---|
| double[] | columnWeights | It is used to hold the overrides to the column weights. |
| int[] | columnWidths | It is used to hold the overrides to the column minimum width. |
| protected Hashtable<Component,GridBagConstraints> | comptable | It is used to maintains the association between a component and its gridbag constraints. |
| protected GridBagConstraints | defaultConstraints | It is used to hold a gridbag constraints instance containing the default values. |
| protected GridBagLayoutInfo | layoutInfo | It is used to hold the layout information for the gridbag. |
| protected static int | MAXGRIDSIZE | No longer in use just for backward compatibility |
| protected static int | MINSIZE | It is smallest grid that can be laid out by the grid bag layout. |
| protected static int | PREFERREDSIZE | It is preferred grid size that can be laid out by the grid bag layout. |
| int[] | rowHeights | It is used to hold the overrides to the row minimum heights. |
| double[] | rowWeights | It is used to hold the overrides to the row weights. |
| Modifier and Type | Method | Description |
|---|---|---|
| void | addLayoutComponent(Component comp, Object constraints) | It adds specified component to the layout, using the specified constraints object. |
| void | addLayoutComponent(String name, Component comp) | It has no effect, since this layout manager does not use a per-component string. |
| protected void | adjustForGravity(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 void | AdjustForGravity(GridBagConstraints constraints, Rectangle r) | This method is for backwards compatibility only |
| protected void | arrangeGrid(Container parent) | Lays out the grid. |
| protected void | ArrangeGrid(Container parent) | This method is obsolete and supplied for backwards compatibility |
| GridBagConstraints | getConstraints(Component comp) | It is for getting the constraints for the specified component. |
| float | getLayoutAlignmentX(Container parent) | It returns the alignment along the x axis. |
| float | getLayoutAlignmentY(Container parent) | It returns the alignment along the y axis. |
| int[][] | getLayoutDimensions() | It determines column widths and row heights for the layout grid. |
| protected GridBagLayoutInfo | getLayoutInfo(Container parent, int sizeflag) | This method is obsolete and supplied for backwards compatibility. |
| protected GridBagLayoutInfo | GetLayoutInfo(Container parent, int sizeflag) | This method is obsolete and supplied for backwards compatibility. |
| Point | getLayoutOrigin() | 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 Dimension | getMinSize(Container parent, GridBagLayoutInfo info) | It figures out the minimum size of the master based on the information from getLayoutInfo. |
| protected Dimension | GetMinSize(Container parent, GridBagLayoutInfo info) | This method is obsolete and supplied for backwards compatibility only |
Implementing GridLayout in Java involves a few simple steps:
1. Importing Packages: Begin by importing the necessary packages:
2. Creating Container: Create a container, such as a JFrame or JPanel, where the components will be placed:
3. Adding Components: Instantiate the UI components and add them to the container:
4. Setting Container Properties: Set properties for the container, such as size and visibility:
FileName: GridBagLayoutExample.java
Output:

FileName: GridBagLayoutDemo.java
Output:

We request you to subscribe our newsletter for upcoming updates.