public class DynamicGridLayout extends Object implements 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 setthis 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"));
Modifier and Type | Field and Description |
---|---|
static int |
ABOVE_MINIMUM
Constants for above minimum size
|
static int |
BELOW_MINIMUM
Constants for below minimum size
|
static int |
BOTTOM
Constants for bottom alignment
|
static int |
CENTER
Constants for center alignment
|
static int |
DYNAMIC
Constants for dynamic size
|
static int |
FILL
Constants for full alignment
|
static int |
LABEL_FILL
Constants for label fill style
|
static int |
LABEL_MIN
Constants for label min style
|
static int |
LEFT
Constants for left alignment
|
static int |
MINIMUM
Constants for minimum size
|
static int |
RIGHT
Constants for right alignment
|
static int |
TOP
Constants for top alignment
|
Constructor and Description |
---|
DynamicGridLayout()
Creates 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.
|
Modifier and Type | Method and Description |
---|---|
void |
addLayoutComponent(String name,
Component comp)
Adds the specified component with the specified name to the layout.
|
int |
getColumns()
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.
|
Dimension |
minimumLayoutSize(Container parent)
Determines the minimum size of the container argument using this
grid layout.
|
Dimension |
preferredLayoutSize(Container parent)
Determines the preferred size of the container argument using
this grid layout.
|
void |
removeLayoutComponent(Component comp)
Removes the specified component from the layout.
|
void |
resetGrid()
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.
|
String |
toString()
Returns the string representation of this grid layout's values.
|
public static final int DYNAMIC
public static final int MINIMUM
public static final int ABOVE_MINIMUM
public static final int BELOW_MINIMUM
public static final int FILL
public static final int CENTER
public static final int LEFT
public static final int RIGHT
public static final int TOP
public static final int BOTTOM
public static final int LABEL_FILL
public static final int LABEL_MIN
public DynamicGridLayout()
public DynamicGridLayout(int rows, int cols)
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.
rows
- the rows, with the value zero meaning
any number of rows.cols
- the columns, with the value zero meaning
any number of columns.public DynamicGridLayout(int rows, int cols, int hgap, int vgap)
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.
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.public int getRows()
public void setRows(int rows)
rows
- the number of rows in this layout.public int getColumns()
public void setColumns(int cols)
cols
- the number of columns in this layout.public int getHgap()
public void setHgap(int hgap)
hgap
- the horizontal gap between components.public int getVgap()
public void setVgap(int vgap)
vgap
- the vertical gap between components.public void resetGrid()
public void setStyle(int style)
The style parameter has the following parameters:
style
- the style to be set.public void setHorizontalAlignment(int value)
value
- the alignment to setpublic void setVerticalAlignment(int value)
value
- the alignment to setpublic void setRowAlignment(int row, int value)
row
- the row to set the alignment forvalue
- the alignment to set.public void setRowAlignments(int start, int end, int value)
start
- the inclusive start row to setend
- the inclusive end row to setvalue
- the alignment to setsetRowAlignment(int, int)
public void setColumnAlignment(int col, int value)
col
- the column to set the alignment forvalue
- the alignment to set.public void setColumnAlignments(int start, int end, int value)
start
- the inclusive start column to setend
- the inclusive end column to setvalue
- the alignment to setsetColumnAlignment(int, int)
public void setRowSize(int row, int value)
row
- The specified number of grid layout.value
- The alignment to set.public void setRowSizes(int start, int end, int value)
start
- the inclusive start row to setend
- the inclusive end row to setvalue
- the size to setsetRowSize(int, int)
public void setColumnSize(int col, int value)
col
- The specified number of grid layout.value
- The alignment to set.public void setColumnSizes(int start, int end, int value)
start
- the inclusive start column to setend
- the inclusive end column to setvalue
- the size to setsetColumnSize(int, int)
public void addLayoutComponent(String name, Component comp)
addLayoutComponent
in interface LayoutManager
name
- the name of the component.comp
- the component to be added.public void removeLayoutComponent(Component comp)
removeLayoutComponent
in interface LayoutManager
comp
- the component to be removed.public Dimension preferredLayoutSize(Container parent)
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.
preferredLayoutSize
in interface LayoutManager
parent
- the container in which to do the layout.GridLayout.minimumLayoutSize(java.awt.Container)
,
Container.getPreferredSize()
public Dimension minimumLayoutSize(Container parent)
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.
minimumLayoutSize
in interface LayoutManager
parent
- the container in which to do the layout.GridLayout.preferredLayoutSize(java.awt.Container)
,
Container.doLayout()
public void layoutContainer(Container parent)
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.
layoutContainer
in interface LayoutManager
parent
- the container in which to do the layout.Container
,
Container.doLayout()