public class GridLayout extends Layout
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.
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:
Constructor and Description |
---|
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
|
GridLayout(int rows,
int columns)
Creates a new instance of GridLayout with the given rows and columns
|
GridLayout(int rows,
int columns,
int landscapeRows,
int landscapeColumns)
Creates a new instance of GridLayout with the given rows and columns
|
Modifier and Type | Method and Description |
---|---|
static GridLayout |
autoFit()
Returns a grid layout that implicitly auto-fits to width in term of number of columns
|
static Container |
encloseIn(Component... cmp)
Creates a new container with an auto fit grid layout and the components added to it
|
static Container |
encloseIn(int columns,
Component... cmp)
Creates a new container with the grid layout and the components added to it
|
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
int |
getColumns() |
Dimension |
getPreferredSize(Container parent)
Returns the container preferred size
|
int |
getRows() |
boolean |
isAutoFit()
Auto fits columns/rows to available screen space
|
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.
|
boolean |
isHideZeroSized()
When set to true components that have 0 size will be hidden and won't occupy a cell within the grid.
|
void |
layoutContainer(Container parent)
Layout the given parent container children
|
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.
|
void |
setAutoFit(boolean autoFit)
Auto fits columns/rows to available screen space
|
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.
|
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.
|
String |
toString()
Returns a string representation of the object.
|
addLayoutComponent, cloneConstraint, getChildrenInTraversalOrder, getComponentConstraint, hashCode, isConstraintTracking, isOverlapSupported, overridesTabIndices, removeLayoutComponent, updateTabIndices
public GridLayout(int rows, int columns)
rows
- - number of rows.columns
- - number of columns.IllegalArgumentException
- if rows < 1 or columns < 1public GridLayout(int rows, int columns, int landscapeRows, int landscapeColumns)
rows
- - number of rows.columns
- - number of columns.landscapeRows
- - number of rows when in landscape modelandscapeColumns
- - number of columns when in landscape modeIllegalArgumentException
- if rows < 1 or columns < 1public GridLayout(int columns)
columns
- - number of columns.IllegalArgumentException
- if rows < 1 or columns < 1public static GridLayout autoFit()
public static Container encloseIn(Component... cmp)
cmp
- the componentspublic static Container encloseIn(int columns, Component... cmp)
columns
- the number of columns for the gridcmp
- the componentspublic void layoutContainer(Container parent)
layoutContainer
in class Layout
parent
- the given parent containerpublic Dimension getPreferredSize(Container parent)
getPreferredSize
in class Layout
parent
- the parent containerpublic String toString()
public int getRows()
public int getColumns()
public boolean equals(Object o)
public boolean isFillLastRow()
public void setFillLastRow(boolean fillLastRow)
fillLastRow
- the fillLastRow to setpublic boolean isAutoFit()
public void setAutoFit(boolean autoFit)
autoFit
- the autoFit to setpublic boolean obscuresPotential(Container parent)
obscuresPotential
in class Layout
parent
- parent containerpublic boolean isHideZeroSized()
public void setHideZeroSized(boolean hideZeroSized)
hideZeroSized
- the hideZeroSized to set