Functions like the GridLayout layout manager, but each column/row can have its own width/height @version 27-Jan-1999 @author Geert Vernaeve
package awt; import java.awt.*; public class GridPackLayout implements LayoutManager, java.io.Serializable { int hgap; int vgap; int rows; int cols;
Creates a grid layout with a default of one column per component, in a single row.
public GridPackLayout() { this(1, 0, 0, 0); }
Creates a grid layout with the specified number of rows and columns.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column. @param rows the rows, with the value zero meaning any number of rows. @param cols the columns, with the value zero meaning any number of columns.

public GridPackLayout(int rows, int cols) { this(rows, cols, 0, 0); }
Creates a grid layout with the specified number of rows and columns.

In addition, the horizontal and vertical gaps are set to the specified values. Horizontal gaps are placed at the left and right edges, and between each of the columns. Vertical gaps are placed at the top and bottom edges, and between each of the rows.

One, but not both, of rows and cols can be zero, which means that any number of objects can be placed in a row or in a column. @param rows the rows, with the value zero meaning any number of rows. @param cols the columns, with the value zero meaning any number of columns. @param hgap the horizontal gap. @param vgap the vertical gap. @exception IllegalArgumentException if the of rows or cols is invalid.

public GridPackLayout(int rows, int cols, int hgap, int vgap) { if (rows==0 && cols==0) { throw new IllegalArgumentException("rows and cols cannot both be zero"); } this.rows = rows; this.cols = cols; this.hgap = hgap; this.vgap = vgap; }
Gets the number of rows in this layout. @return the number of rows in this layout.
public int getRows() { return rows; }
Sets the number of rows in this layout to the specified value. @param rows the number of rows in this layout. @exception IllegalArgumentException if the value of both rows and cols is set to zero.
public void setRows(int rows) { if (rows==0 && cols==0) { throw new IllegalArgumentException("rows and cols cannot both be zero"); } this.rows = rows; }
Gets the number of columns in this layout. @return the number of columns in this layout.
public int getColumns() { return cols; }
Sets the number of columns in this layout to the specified value. @param cols the number of columns in this layout. @exception IllegalArgumentException if the value of both rows and cols is set to zero.
public void setColumns(int cols) { if (cols==0 && rows==0) { throw new IllegalArgumentException("rows and cols cannot both be zero"); } this.cols = cols; }
Gets the horizontal gap between components. @return the horizontal gap between components.
public int getHgap() { return hgap; }
Sets the horizontal gap between components to the specified value. @param hgap the horizontal gap between components.
public void setHgap(int hgap) { this.hgap = hgap; }
Gets the vertical gap between components. @return the vertical gap between components.
public int getVgap() { return vgap; }
Sets the vertical gap between components to the specified value. @param vgap the vertical gap between components.
public void setVgap(int vgap) { this.vgap = vgap; }
Adds the specified component with the specified name to the layout. @param name the name of the component. @param comp the component to be added.
public void addLayoutComponent(String name, Component comp) {}
Removes the specified component from the layout. @param comp the component to be removed.
public void removeLayoutComponent(Component comp) {}
Determines the preferred size of the container argument using this grid layout.

The preferred width of a grid layout is the sum of the widths of the columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.

The preferred height of a grid layout is the sum of the heights of the rows, plus the vertical padding times the number of rows plus one, plus the top and left insets of the target container. @param target the container in which to do the layout. @return the preferred dimensions to lay out the subcomponents of the specified container. @see java.awt.GridLayout#minimumLayoutSize @see java.awt.Container#getPreferredSize() @since JDK1.0

public Dimension preferredLayoutSize(Container parent) { synchronized (parent.getTreeLock()){ int ncomponents = parent.getComponentCount(); int nrows = rows; int ncols = cols; if (nrows > 0) ncols = (ncomponents + nrows - 1) / nrows; else nrows = (ncomponents + ncols - 1) / ncols; int w = 0; // width for (int x = 0; x < ncols; x++) { // width of column x int colwidth = 0; int index = x; for (int y = 0; y < nrows; y++) { Component comp = parent.getComponent(index); int width = comp.getPreferredSize().width; if (width > colwidth) colwidth = width; index += ncols; if (index >= ncomponents) break; } w += colwidth; } int h = 0; // height for (int y = 0 ; y < nrows ; y++) { // height of row y int colheight = 0; int index = y * ncols; for (int x = 0; x < ncols; x++) { Component comp = parent.getComponent(index); int height = comp.getPreferredSize().height; if (height > colheight) colheight = height; index++; if (index >= ncomponents) break; } h += colheight; } Insets insets = parent.getInsets(); return new Dimension(insets.left + insets.right + w + (ncols-1)*hgap, insets.top + insets.bottom + h + (nrows-1)*vgap); } }
Determines the minimum size of the container argument using this grid layout. @param target the container in which to do the layout. @return the minimum dimensions needed to lay out the subcomponents of the specified container. @see java.awt.GridLayout#preferredLayoutSize @see java.awt.Container#doLayout
public Dimension minimumLayoutSize(Container parent) { synchronized (parent.getTreeLock()){ int ncomponents = parent.getComponentCount(); int nrows = rows; int ncols = cols; if (nrows > 0) ncols = (ncomponents + nrows - 1) / nrows; else nrows = (ncomponents + ncols - 1) / ncols; int w = 0; // width { int [] widths = colMinWidths(parent); for (int x = 0; x < ncols; x++) w += widths[x]; } int h = 0; // height for (int y = 0 ; y < nrows ; y++) { // height of row y int colheight = 0; int index = y * ncols; for (int x = 0; x < nrows; x++) { Component comp = parent.getComponent(index); int height = comp.getMinimumSize().height; if (height > colheight) colheight = height; index++; if (index >= ncomponents) break; } h += colheight; } Insets insets = parent.getInsets(); return new Dimension(insets.left + insets.right + w + (ncols-1)*hgap, insets.top + insets.bottom + h + (nrows-1)*vgap); } } private int [] colMinWidths(Container parent) { synchronized (parent.getTreeLock()){ int ncomponents = parent.getComponentCount(); int nrows = rows; int ncols = cols; if (nrows > 0) ncols = (ncomponents + nrows - 1) / nrows; else nrows = (ncomponents + ncols - 1) / ncols; int [] result = new int[ncols]; // width for (int x = 0; x < ncols; x++) { // width of column x int colwidth = 0; int index = x; for (int y = 0; y < nrows; y++) { Component comp = parent.getComponent(index); int width = comp.getMinimumSize().width; if (width > colwidth) colwidth = width; index += ncols; if (index >= ncomponents) break; } result[x] = colwidth; } return result; } }
Lays out the specified container using this layout.

This method reshapes the components in the specified target container in order to satisfy the constraints of the GridLayout object.

The grid layout manager determines the size of individual components by dividing the free space in the container into portions according to the number of rows and columns in the layout. The container's free space equals the container's size minus any insets and any specified horizontal or vertical gap. @param target the container in which to do the layout. @see java.awt.Container @see java.awt.Container#doLayout

public void layoutContainer(Container parent) { synchronized (parent.getTreeLock()) { Insets insets = parent.getInsets(); int ncomponents = parent.getComponentCount(); int nrows = rows; int ncols = cols; if (ncomponents == 0) return; if (nrows > 0) ncols = (ncomponents + nrows - 1) / nrows; else nrows = (ncomponents + ncols - 1) / ncols; int [] widths = colMinWidths(parent); int index = 0; int ypos = insets.top; for (int y = 0; y < nrows; y++) { // Layout row y int xpos = insets.left; int rowheight = 0; // Calculate row height for (int x = 0; x < ncols; x++) { if (index+x >= ncomponents) break; Dimension dim = parent.getComponent(index+x).getMinimumSize(); if (dim.height > rowheight) rowheight = dim.height; } for (int x = 0; x < ncols; x++) { if (index >= ncomponents) break; Component comp = parent.getComponent(index++); comp.setBounds(xpos,ypos,widths[x],rowheight); //System.out.println((index-1)+" "+xpos+" "+ypos+" "+widths[x]+" "+rowheight); xpos += widths[x] + hgap; } ypos += rowheight + vgap; } } }
Returns the string representation of this grid layout's values. @return a string representation of this grid layout.
public String toString() { return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + ",rows=" + rows + ",cols=" + cols + "]"; } }