Class GridLayout
The components are arranged in a grid based on available space, all cells in the grid are given exactly the same size which matches the largest preferred size or available space. The main use case for this layout is a grid of icons e.g. like one would see in the iPhone home screen.
If the number of rows * columns is smaller than the number of components added a new row is implicitly added to the grid. However, if the number of components is smaller than available cells (won't fill the last row) blank spaces will be left in place.
In this example we can see that a 2x2 grid is used to add 5 elements, this results in an additional row that's implicitly added turning the grid to a 3x2 grid implicitly and leaving one blank cell.
Form hi = new Form("Grid Layout 2x2", new GridLayout(2, 2));
hi.add(new Label("First")).
add(new Label("Second")).
add(new Label("Third")).
add(new Label("Fourth")).
add(new Label("Fifth"));
When we use a 2x4 size ratio we would see elements getting cropped as we do here. The grid layout uses the grid size first and doesn't pay too much attention to the preferred size of the components it holds.
Grid also has an autoFit attribute that can be used to automatically calculate the column count based on available space and preferred width. This is really useful for working with UI's where the device orientation might change.
There is also a terse syntax for working with a grid that has two versions, one that uses the "auto fit" option and another that accepts the column names. Heres a sample of the terse syntax coupled with the auto fit screenshots of the same code in two orientations:
GridLayout.encloseIn(new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth")));
-
Constructor Summary
ConstructorsConstructorDescriptionGridLayout(int columns) Creates a new instance of GridLayout with the given columns, rows is set to 1 but will implicitly grow if more components are addedGridLayout(int rows, int columns) Creates a new instance of GridLayout with the given rows and columnsGridLayout(int rows, int columns, int landscapeRows, int landscapeColumns) Creates a new instance of GridLayout with the given rows and columns -
Method Summary
Modifier and TypeMethodDescriptionstatic GridLayoutautoFit()Returns a grid layout that implicitly auto-fits to width in term of number of columnsstatic ContainerCreates a new container with the grid layout and the components added to itstatic ContainerCreates a new container with an auto fit grid layout and the components added to itbooleanintReturns
getPreferredSize(Container parent) Returns the container preferred sizeintgetRows()Returns
inthashCode()booleanAuto fits columns/rows to available screen spacebooleanWhen set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.booleanWhen set to true components that have 0 size will be hidden and won't occupy a cell within the grid.voidlayoutContainer(Container parent) Layout the given parent container childrenbooleanobscuresPotential(Container parent) Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that.voidsetAutoFit(boolean autoFit) Auto fits columns/rows to available screen spacevoidsetFillLastRow(boolean fillLastRow) When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.voidsetHideZeroSized(boolean hideZeroSized) When set to true components that have 0 size will be hidden and won't occupy a cell within the grid.toString()Methods inherited from class Layout
addLayoutComponent, cloneConstraint, getChildrenInTraversalOrder, getComponentConstraint, isConstraintTracking, isOverlapSupported, overridesTabIndices, removeLayoutComponent, updateTabIndices
-
Constructor Details
-
GridLayout
public GridLayout(int rows, int columns) Creates a new instance of GridLayout with the given rows and columns
Parameters
-
rows: - number of rows. -
columns: - number of columns.
Throws
IllegalArgumentException: if rows < 1 or columns < 1
-
-
GridLayout
public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns) Creates a new instance of GridLayout with the given rows and columns
Parameters
-
rows: - number of rows. -
columns: - number of columns. -
landscapeRows: - number of rows when in landscape mode -
landscapeColumns: - number of columns when in landscape mode
Throws
IllegalArgumentException: if rows < 1 or columns < 1
-
-
GridLayout
public GridLayout(int columns) Creates a new instance of GridLayout with the given columns, rows is set to 1 but will implicitly grow if more components are added
Parameters
columns: - number of columns.
Throws
IllegalArgumentException: if rows < 1 or columns < 1
-
-
Method Details
-
autoFit
Returns a grid layout that implicitly auto-fits to width in term of number of columns
Returns
a grid layout that automatically adapts its size
-
encloseIn
-
encloseIn
-
layoutContainer
Layout the given parent container children
Parameters
parent: the given parent container
- Specified by:
layoutContainerin classLayout
-
getPreferredSize
Returns the container preferred size
Parameters
parent: the parent container
Returns
the container preferred size
- Specified by:
getPreferredSizein classLayout
-
toString
-
getRows
public int getRows()Returns
the rows
-
getColumns
public int getColumns()Returns
the columns
-
equals
-
hashCode
-
isFillLastRow
public boolean isFillLastRow()When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.
Returns
the fillLastRow
-
setFillLastRow
public void setFillLastRow(boolean fillLastRow) When set to true makes the grid layout fill the last row of the layout entirely if the number of elements in that row is bigger.
Parameters
fillLastRow: the fillLastRow to set
-
isAutoFit
public boolean isAutoFit()Auto fits columns/rows to available screen space
Returns
the autoFit
-
setAutoFit
public void setAutoFit(boolean autoFit) Auto fits columns/rows to available screen space
Parameters
autoFit: the autoFit to set
-
obscuresPotential
Some layout managers can obscure their child components in some cases this returns true if the basic underpinnings are in place for that. This method doesn't take padding/margin etc. into account since that is checked by the caller
Parameters
parent: parent container
Returns
true if there is a chance that this layout manager can fully obscure the background, when in doubt return false...
- Overrides:
obscuresPotentialin classLayout
-
isHideZeroSized
public boolean isHideZeroSized()When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.
Returns
the hideZeroSized
-
setHideZeroSized
public void setHideZeroSized(boolean hideZeroSized) When set to true components that have 0 size will be hidden and won't occupy a cell within the grid. This makes animating a grid layout component MUCH easier.
Parameters
hideZeroSized: the hideZeroSized to set
-