public 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"));
Constructors
public GridLayout(int rows, int columns) | Creates a new instance of GridLayout with the given rows and columns |
public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns) | Creates a new instance of GridLayout with the given rows and columns |
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 |
Methods
public static GridLayout autoFit() | Returns a grid layout that implicitly auto-fits to width in term of number of columns |
public static Container encloseIn(Component... cmp) | Creates a new container with an auto fit grid layout and the components added to it |
public static Container encloseIn(int columns, Component... cmp) | Creates a new container with the grid layout and the components added to it |
public void layoutContainer(Container parent) | Layout the given parent container children |
public Dimension getPreferredSize(Container parent) | Returns the container preferred size |
public String toString() | Returns a string representation of the object. |
public int getRows() | |
public int getColumns() | |
public boolean equals(Object o) | Indicates whether some other object is “equal to” this one. |
public int hashCode() | Returns a hash code value for the object. |
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. |
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. |
public boolean isAutoFit() | Auto fits columns/rows to available screen space |
public void setAutoFit(boolean autoFit) | Auto fits columns/rows to available screen space |
public boolean obscuresPotential(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. |
public boolean isHideZeroSized() | When set to true components that have 0 size will be hidden and won’t occupy a cell within the grid. |
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. |
Inherited methods
Constructor details
GridLayout
public GridLayout(int rows, int columns)Parameters
rowsint- number of rows.
columnsint- number of columns.
Throws
IllegalArgumentException- if rows < 1 or columns < 1
GridLayout
public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns)Parameters
rowsint- number of rows.
columnsint- number of columns.
landscapeRowsint- number of rows when in landscape mode
landscapeColumnsint- number of columns when in landscape mode
Throws
IllegalArgumentException- if rows < 1 or columns < 1
GridLayout
public GridLayout(int columns)Parameters
columnsint- number of columns.
Throws
IllegalArgumentException- if rows < 1 or columns < 1
Method details
autoFit
public static GridLayout autoFit()Returns
encloseIn
public static Container encloseIn(Component... cmp)Parameters
cmpComponent...- the components
Returns
encloseIn
public static Container encloseIn(int columns, Component... cmp)Parameters
columnsint- the number of columns for the grid
cmpComponent...- the components
Returns
layoutContainer
public void layoutContainer(Container parent)Parameters
parentContainer- the given parent container
getPreferredSize
public Dimension getPreferredSize(Container parent)Parameters
parentContainer- the parent container
Returns
toString
public String toString()getRows
public int getRows()Returns
getColumns
public int getColumns()Returns
equals
public boolean equals(Object o)hashCode
public int hashCode()isFillLastRow
public boolean isFillLastRow()Returns
setFillLastRow
public void setFillLastRow(boolean fillLastRow)Parameters
fillLastRowboolean- the fillLastRow to set
isAutoFit
public boolean isAutoFit()Returns
setAutoFit
public void setAutoFit(boolean autoFit)Parameters
autoFitboolean- the autoFit to set
obscuresPotential
public boolean obscuresPotential(Container parent)Parameters
parentContainer- parent container
Returns
isHideZeroSized
public boolean isHideZeroSized()Returns
setHideZeroSized
public void setHideZeroSized(boolean hideZeroSized)Parameters
hideZeroSizedboolean- the hideZeroSized to set