Class DynamicGridLayout
- All Implemented Interfaces:
LayoutManager
,Serializable
As with GridLayout one component is placed in each rectangle, but the width of columns and height of rows are not necessarily the same. By default each row and column will be sized in proportion to the minimum size of the largest component in that row or column. Alternatively, individual rows or columns can be set to a fixed percentage of the container or their minimum size with various options. Components can also be set to fill the assigned area or use their minimum size and align themselves within that allocated space. If the grid itself doesn't completely fill the area assigned by the container there are methods to set this alignment too.
Here's a simple example of using a DynamicGridLayout.
//create the layout and set it setLayout(dgl=new DynamicGridLayout(2,2,5,5)); //set any styles, sizes or alignments dgl.setRowSize(0,dgl.MINIMUM); //add the components add(new Button("Row One/Col One")); add(new Button("Row One/Col Two")); add(new Button("Row Two/Col One")); add(new Button("Row Two/Col Two"));
- Version:
- $Revision: 1.2 $
- Author:
- Robert Nielsen
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
static final int
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a grid layout with a default of one column per component, in a single row.DynamicGridLayout
(int rows, int cols) Creates a grid layout with the specified number of rows and columns.DynamicGridLayout
(int rows, int cols, int hgap, int vgap) Creates a grid layout with the specified number of rows and columns. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addLayoutComponent
(String name, Component comp) Adds the specified component with the specified name to the layout.int
Gets the number of columns in this layout.int
getHgap()
Gets the horizontal gap between components.int
getRows()
Gets the number of rows in this layout.int
getVgap()
Gets the vertical gap between components.void
layoutContainer
(Container parent) Lays out the specified container using this layout.minimumLayoutSize
(Container parent) Determines the minimum size of the container argument using this grid layout.preferredLayoutSize
(Container parent) Determines the preferred size of the container argument using this grid layout.void
Removes the specified component from the layout.void
Resets the grid parameters.void
setColumnAlignment
(int col, int value) Sets the alignment for a particular column.void
setColumnAlignments
(int start, int end, int value) Sets the alignment of multiple columns.void
setColumns
(int cols) Sets the number of columns in this layout to the specified value.void
setColumnSize
(int col, int value) Sets the size of a column.void
setColumnSizes
(int start, int end, int value) Sets the size of multiple columns.void
setHgap
(int hgap) Sets the horizontal gap between components to the specified value.void
setHorizontalAlignment
(int value) This method is only required if there are no columns with dynamic widths which may cause the layout to be smaller than the allocated space.void
setRowAlignment
(int row, int value) Sets the alignment for a particular row.void
setRowAlignments
(int start, int end, int value) Sets the alignment of multiple rows.void
setRows
(int rows) Sets the number of rows in this layout to the specified value.void
setRowSize
(int row, int value) Sets the size of a row.void
setRowSizes
(int start, int end, int value) Sets the size of multiple rows.void
setStyle
(int style) Sets the style of this grid.void
setVerticalAlignment
(int value) This method is only required if there are no rows with dynamic heights which may cause the layout to be smaller than the allocated space.void
setVgap
(int vgap) Sets the vertical gap between components to the specified value.toString()
Returns the string representation of this grid layout's values.
-
Field Details
-
DYNAMIC
public static final int DYNAMIC- See Also:
-
MINIMUM
public static final int MINIMUM- See Also:
-
ABOVE_MINIMUM
public static final int ABOVE_MINIMUM- See Also:
-
BELOW_MINIMUM
public static final int BELOW_MINIMUM- See Also:
-
FILL
public static final int FILL- See Also:
-
CENTER
public static final int CENTER- See Also:
-
LEFT
public static final int LEFT- See Also:
-
RIGHT
public static final int RIGHT- See Also:
-
TOP
public static final int TOP- See Also:
-
BOTTOM
public static final int BOTTOM- See Also:
-
LABEL_FILL
public static final int LABEL_FILL- See Also:
-
LABEL_MIN
public static final int LABEL_MIN- See Also:
-
-
Constructor Details
-
DynamicGridLayout
public DynamicGridLayout()Creates a grid layout with a default of one column per component, in a single row. -
DynamicGridLayout
public DynamicGridLayout(int rows, int cols) Creates a grid layout with the specified number of rows and columns. All components in the layout are given equal size.One, but not both, of
rows
andcols
can be zero, which means that any number of objects can be placed in a row or in a column.- Parameters:
rows
- the rows, with the value zero meaning any number of rows.cols
- the columns, with the value zero meaning any number of columns.
-
DynamicGridLayout
public DynamicGridLayout(int rows, int cols, int hgap, int vgap) 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
andcols
can be zero, which means that any number of objects can be placed in a row or in a column.- Parameters:
rows
- the rows, with the value zero meaning any number of rows.cols
- the columns, with the value zero meaning any number of columns.hgap
- the horizontal gap.vgap
- the vertical gap.- Throws:
IllegalArgumentException
- if the ofrows
orcols
is invalid.
-
-
Method Details
-
getRows
public int getRows()Gets the number of rows in this layout.- Returns:
- the number of rows in this layout.
-
setRows
public void setRows(int rows) Sets the number of rows in this layout to the specified value.- Parameters:
rows
- the number of rows in this layout.- Throws:
IllegalArgumentException
- if the value of bothrows
andcols
is set to zero.
-
getColumns
public int getColumns()Gets the number of columns in this layout.- Returns:
- the number of columns in this layout.
- Since:
- JDK1.1
-
setColumns
public void setColumns(int cols) Sets the number of columns in this layout to the specified value.- Parameters:
cols
- the number of columns in this layout.- Throws:
IllegalArgumentException
- if the value of bothrows
andcols
is set to zero.
-
getHgap
public int getHgap()Gets the horizontal gap between components.- Returns:
- the horizontal gap between components.
-
setHgap
public void setHgap(int hgap) Sets the horizontal gap between components to the specified value.- Parameters:
hgap
- the horizontal gap between components.
-
getVgap
public int getVgap()Gets the vertical gap between components.- Returns:
- the vertical gap between components.
-
setVgap
public void setVgap(int vgap) Sets the vertical gap between components to the specified value.- Parameters:
vgap
- the vertical gap between components.
-
resetGrid
public void resetGrid()Resets the grid parameters. All styles, alignments and sizes are reset to their default values. -
setStyle
public void setStyle(int style) Sets the style of this grid. This provides a shortcut to producing several standard styles of grid layout by setting various row alignments and sizes. The style can be tweaked by using the set(Row|Column)Alignment and set(Row|Column)Size methods after a call to this one.The style parameter has the following parameters:
- FILL to use all available space, with component heights and widths relative to their preferred size.
- CENTER to use the same spacing as FILL but with each component being it's preferred size and centered in the space.
- LABEL_FILL is a simple layout where the 1st, 3rd, etc. columns are considered to be labels and the intervening ones are the data components (TextFields, ComboBoxes). The labels are minimum size and right aligned and the others fill all available space.
- LABEL_MIN is the same as LABEL_FILL but the data components are minimum too and aligned left.
- Parameters:
style
- the style to be set.
-
setHorizontalAlignment
public void setHorizontalAlignment(int value) This method is only required if there are no columns with dynamic widths which may cause the layout to be smaller than the allocated space. The horizontal alignment of the grid in this space can then be specified using this method. The default is centered. The available alignments are:- LEFT - align the grid to the left of the space available.
- CENTER - center the grid in the space.
- RIGHT - align the grid to the right of the space available.
- Parameters:
value
- the alignment to set
-
setVerticalAlignment
public void setVerticalAlignment(int value) This method is only required if there are no rows with dynamic heights which may cause the layout to be smaller than the allocated space. The vertical alignment of the grid in this space can then be specified using this method. The default is centered. The available alignments are:- TOP - align the grid to the top of the space available.
- CENTER - center the grid in the space.
- BOTTOM - align the grid to the bottom of the space available.
- Parameters:
value
- the alignment to set
-
setRowAlignment
public void setRowAlignment(int row, int value) Sets the alignment for a particular row. If the alignment is not FILL, the preferred size of the component will be used unless it is greater than the available space, in which case it will be truncated. The default alignment is FILL for all rows. The available alignments are:- FILL - fill the entire vertical space available.
- TOP - align to the top of the space available.
- CENTER - center the component in the space.
- BOTTOM - align with the bottom of the space available.
- Parameters:
row
- the row to set the alignment forvalue
- the alignment to set.
-
setRowAlignments
public void setRowAlignments(int start, int end, int value) Sets the alignment of multiple rows. See setRowAlignment for details of alignment values.- Parameters:
start
- the inclusive start row to setend
- the inclusive end row to setvalue
- the alignment to set- See Also:
-
setColumnAlignment
public void setColumnAlignment(int col, int value) Sets the alignment for a particular column. If the alignment is not FILL, the preferred size of the component will be used unless it is greater than the available space, in which case it will be truncated. The default alignment is FILL for all columns. The available alignments are:- FILL - fill the entire vertical space available.
- LEFT - align to the left of the space available.
- CENTER - center the component in the space.
- RIGHT - align to the right of the space available.
- Parameters:
col
- the column to set the alignment forvalue
- the alignment to set.
-
setColumnAlignments
public void setColumnAlignments(int start, int end, int value) Sets the alignment of multiple columns. See setColumnAlignment for details of alignment values.- Parameters:
start
- the inclusive start column to setend
- the inclusive end column to setvalue
- the alignment to set- See Also:
-
setRowSize
public void setRowSize(int row, int value) Sets the size of a row. The available sizes are:- percentage - a value between 1 and 100 for the percentage of the width of the grid that this row should take up.
- MINIMUM - make sure that the height of this row is always the minimum height of of the largest component in the row no matter what it does to the formatting. It can look ugly if you resize the window too small but is good for labels which must always display all their text.
- DYNAMIC - After the percentages and minimum components are removed from the grid width the rows with dynamic sizes are allocated a proportion of the remaining space based on the minimum height of the largest component in the row. This is the default size for all rows.
- BELOW_MINIMUM - the height is set to MINIMUM unless this minimum size would be smaller than a dynamically allocated one, in which case DYNAMIC allocation is used. This setting is useful if you want a component to stick to it's minimum size in most situations but go even smaller if the panel size gets really small.
- ABOVE_MINIMUM - the height is allocated using the DYNAMIC setting unless it would make the component smaller than it's minimum size, where MINIMUM is used. This option is useful if you want a component to take up any available space but never go smaller than it's minimum size.
- Parameters:
value
-
-
setRowSizes
public void setRowSizes(int start, int end, int value) Sets the size of multiple rows. See setRowSize for details of alignment values.- Parameters:
start
- the inclusive start row to setend
- the inclusive end row to setvalue
- the size to set- See Also:
-
setColumnSize
public void setColumnSize(int col, int value) Sets the size of a column. The available sizes are:- percentage - a value between 1 and 100 for the percentage of the width of the grid that this column should take up.
- MINIMUM - make sure that the width of this column is always the minimum width of the largest component in the column no matter what it does to the formatting. It can look ugly if you resize the window too small but is good for labels which must always display all their text.
- DYNAMIC - After the percentages and minimum components are removed from the grid width the columns with dynamic sizes are allocated a proportion of the remaining space based on the minimum width of the largest component in the row. This is the default size for all column.
- BELOW_MINIMUM - the height is set to MINIMUM unless this minimum size would be smaller than a dynamically allocated one, in which case DYNAMIC allocation is used. This setting is useful if you want a component to stick to it's minimum size in most situations but go even smaller if the panel size gets really small.
- ABOVE_MINIMUM - the width is allocated using the DYNAMIC setting unless it would make the component smaller than it's minimum size, where MINIMUM is used. This option is useful if you want a component to take up any available space but never go smaller than it's minimum size.
- Parameters:
value
-
-
setColumnSizes
public void setColumnSizes(int start, int end, int value) Sets the size of multiple columns. See setColumnSize for details of alignment values.- Parameters:
start
- the inclusive start column to setend
- the inclusive end column to setvalue
- the size to set- See Also:
-
addLayoutComponent
Adds the specified component with the specified name to the layout.- Specified by:
addLayoutComponent
in interfaceLayoutManager
- Parameters:
name
- the name of the component.comp
- the component to be added.
-
removeLayoutComponent
Removes the specified component from the layout.- Specified by:
removeLayoutComponent
in interfaceLayoutManager
- Parameters:
comp
- the component to be removed.
-
preferredLayoutSize
Determines the preferred size of the container argument using this grid layout.The preferred width of a grid layout is the largest preferred width of any of the widths in the container times the number of 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 largest preferred height of any of the heights in the container times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.
- Specified by:
preferredLayoutSize
in interfaceLayoutManager
- Parameters:
parent
- the container in which to do the layout.- Returns:
- the preferred dimensions to lay out the subcomponents of the specified container.
- See Also:
-
minimumLayoutSize
Determines the minimum size of the container argument using this grid layout.The minimum width of a grid layout is the largest minimum width of any of the widths in the container times the number of columns, plus the horizontal padding times the number of columns plus one, plus the left and right insets of the target container.
The minimum height of a grid layout is the largest minimum height of any of the heights in the container times the number of rows, plus the vertical padding times the number of rows plus one, plus the top and bottom insets of the target container.
- Specified by:
minimumLayoutSize
in interfaceLayoutManager
- Parameters:
parent
- the container in which to do the layout.- Returns:
- the minimum dimensions needed to lay out the subcomponents of the specified container.
- See Also:
-
layoutContainer
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 equal-sized 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. All components in a grid layout are given the same size.
- Specified by:
layoutContainer
in interfaceLayoutManager
- Parameters:
parent
- the container in which to do the layout.- See Also:
-
toString
-