java.awt

Class GridLayout

public class GridLayout extends Object implements LayoutManager, Serializable

This class implements a grid-based layout scheme. Components are all given the same size and are laid out from left to right and top to bottom. A GridLayout is configured with a number of rows and a number of columns. If both are specified, then the number of columns is ignored and is derived from the number of rows and the total number of components. If either is zero then that dimension is computed based on the actual size of the container. An exception is thrown if an attempt is made to set both the number of rows and the number of columns to 0. This class also supports horizontal and vertical gaps; these are used as spacing between cells.
Constructor Summary
GridLayout()
Create a new GridLayout with one row and any number of columns.
GridLayout(int rows, int cols)
Create a new GridLayout with the specified number of rows and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
Create a new GridLayout with the specified number of rows and columns and the specified gaps.
Method Summary
voidaddLayoutComponent(String name, Component comp)
Add a new component to the layout.
intgetColumns()
Return the number of columns in this layout.
intgetHgap()
Return the horizontal gap.
intgetRows()
Return the number of rows in this layout.
intgetVgap()
Return the vertical gap.
voidlayoutContainer(Container parent)
Lay out the container's components based on current settings.
DimensionminimumLayoutSize(Container cont)
Get the minimum layout size of the container.
DimensionpreferredLayoutSize(Container cont)
Get the preferred layout size of the container.
voidremoveLayoutComponent(Component comp)
Remove the indicated component from this layout manager.
voidsetColumns(int newCols)
Set the number of columns.
voidsetHgap(int hgap)
Set the horizontal gap.
voidsetRows(int newRows)
Set the number of rows
voidsetVgap(int vgap)
Set the vertical gap.
StringtoString()
Return String description of this object.

Constructor Detail

GridLayout

public GridLayout()
Create a new GridLayout with one row and any number of columns. Both gaps are set to 0.

GridLayout

public GridLayout(int rows, int cols)
Create a new GridLayout with the specified number of rows and columns. Both gaps are set to 0. Note that the row and column settings cannot both be zero. If both the row and column values are non-zero, the rows value takes precedence.

Parameters: rows Number of rows cols Number of columns

Throws: IllegalArgumentException If rows and columns are both 0, or if either are negative

GridLayout

public GridLayout(int rows, int cols, int hgap, int vgap)
Create a new GridLayout with the specified number of rows and columns and the specified gaps. Note that the row and column settings cannot both be zero. If both the row and column values are non-zero, the rows value takes precedence.

Parameters: rows Number of rows cols Number of columns hgap The horizontal gap vgap The vertical gap

Throws: IllegalArgumentException If rows and columns are both 0, if either are negative, or if either gap is negative

Method Detail

addLayoutComponent

public void addLayoutComponent(String name, Component comp)
Add a new component to the layout. This particular implementation does nothing.

Parameters: name The name of the component to add. comp The component to add.

getColumns

public int getColumns()
Return the number of columns in this layout.

getHgap

public int getHgap()
Return the horizontal gap.

getRows

public int getRows()
Return the number of rows in this layout.

getVgap

public int getVgap()
Return the vertical gap.

layoutContainer

public void layoutContainer(Container parent)
Lay out the container's components based on current settings. The free space in the container is divided evenly into the specified number of rows and columns in this object.

Parameters: parent The container to lay out

minimumLayoutSize

public Dimension minimumLayoutSize(Container cont)
Get the minimum layout size of the container.

Parameters: cont The parent container

preferredLayoutSize

public Dimension preferredLayoutSize(Container cont)
Get the preferred layout size of the container.

Parameters: cont The parent container

removeLayoutComponent

public void removeLayoutComponent(Component comp)
Remove the indicated component from this layout manager. This particular implementation does nothing.

Parameters: comp The component to remove

setColumns

public void setColumns(int newCols)
Set the number of columns.

Parameters: newCols

Throws: IllegalArgumentException If the number of columns is negative, or if the number of columns is zero and the number of rows is already 0.

setHgap

public void setHgap(int hgap)
Set the horizontal gap. An Exception is not thrown if hgap < 0.

Parameters: hgap The horizontal gap

setRows

public void setRows(int newRows)
Set the number of rows

Parameters: newRows

Throws: IllegalArgumentException If the number of rows is negative, or if the number of rows is zero and the number of columns is already 0.

setVgap

public void setVgap(int vgap)
Set the vertical gap. An Exception is not thrown if vgap < 0.

Parameters: vgap The vertical gap

toString

public String toString()
Return String description of this object.