#

| Chapter 19 java.awt Reference |

# GridLayout

## Name

GridLayout

##

## Description

The `GridLayout` `LayoutManager` provides the means to layout components in a grid of rows and columns.

## Class Definition

    public class java.awt.GridLayout
            extends java.lang.Object
            implements java.awt.LayoutManager, java.io.Serializable
     {

    // Constructors
      public GridLayout();
      public GridLayout (int rows, int cols);
      public GridLayout (int rows, int cols, int hgap, int vgap);

    // Instance Methods
      public void addLayoutComponent (String name, Component component);
      public int getColumns();
      public int getHgap();
      public int getRows();

      public int getVgap();
      public void layoutContainer (Container target);
      public Dimension minimumLayoutSize (Container target);
      public Dimension preferredLayoutSize (Container target);
      public void removeLayoutComponent (Component component);
      public int setColumns(int cols);
      public int setHgap(int hgap);
      public int setRows(int rows);

      public int setVgap(int vgap);
      public String toString();
    }

## Constructors

### GridLayout

#### public GridLayout()

Description

  Constructs a `GridLayout` object with a default single row and one column per component.

#### public GridLayout (int rows, int cols)

Parameters

  rows

    Requested number of rows in container.

  cols

    Requested number of columns in container.

Description

  Constructs a `GridLayout` object with the requested number of rows and columns. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor's parameters.

#### public GridLayout (int rows, int cols, int hgap, int vgap)

Parameters

  rows

    Requested number of rows in container.

  cols

    Requested number of columns in container.

  hgap

    Horizontal space between each component in a row.

  vgap

    Vertical space between each row.

Description

  Constructs a `GridLayout` object with the requested number of `rows` and `columns` and the values specified as the gaps between each component. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor's parameters.

## Instance Methods

### addLayoutComponent

#### public void addLayoutComponent (String name, Component component)

Parameters

  name

    Name of component to add.

  component

    Actual component being added.

Implements

  `LayoutManager.addLayoutComponent()`

Description

  Does nothing.

### getColumns

#### public int getColumns()

Returns

  The number of columns.

### getHgap

#### public int getHgap()

Returns

  The horizontal gap for this `GridLayout` instance.

### getRows

#### public int getRows()

Returns

  The number of rows.

### getVgap

#### public int getVgap()

Returns

  The vertical gap for this `GridLayout` instance.

### layoutContainer

#### public void layoutContainer (Container target)

Parameters

  target

    The container that needs to be redrawn.

Implements

  `LayoutManager.layoutContainer()`

Description

  Draws the components contained within the `target`.

### minimumLayoutSize

#### public Dimension minimumLayoutSize (Container target)

Parameters

  target

    The container whose size needs to be calculated.

Returns

  Minimum `Dimension` of the container `target`.

Implements

  `LayoutManager.minimumLayoutSize()`

Description

  Calculates the minimum size of the `target` container.

### preferredLayoutSize

#### public Dimension preferredLayoutSize (Container target)

Parameters

  target

    The container whose size needs to be calculated.

Returns

  Preferred `Dimension` of the container `target`.

Implements

  `LayoutManager.preferredLayoutSize()`

Description

  Calculates the preferred size of the `target` container.

### removeLayoutComponent

#### public void removeLayoutComponent (Component component)

Parameters

  component

    Component to stop tracking.

Implements

  `LayoutManager.removeLayoutComponent()`

Description

  Does nothing.

### setColumns

#### public void setColumns(int cols)

Parameters

  cols

    The new number of columns.

Description

  Sets the number of columns.

### setHgap

#### public void setHgap(int hgap)

Parameters

  hgap

    The horizontal gap value.

Description

  Sets the horizontal gap between components.

### setRows

#### public void setRows(int rows)

Parameters

  rows

    The new number of rows.

Description

  Sets the number of rows.

### setVgap

#### public void setVgap(int vgap)

Parameters

  vgap

    The vertical gap value.

Description

  Sets the vertical gap between components.

### toString

#### public String toString()

Returns

  A string representation of the `GridLayout` object.

Overrides

  `Object.toString()`

## See Also

`Component`, `Container`, `Dimension`, `LayoutManager`, `Object`, `String`

GridBagLayout |  | IllegalComponentStateException