public class TableLayout extends Layout
TableLayout is a very elaborate constraint based layout manager that can arrange elements
in rows/columns while defining constraints to control complex behavior such as spanning, alignment/weight
etc.
Notice that the table layout is in the com.codename1.ui.table
package and not in the
layouts package.
This is due to the fact that TableLayout
was originally designed for the
Table
class.
Despite being constraint based the table layout isn't strict about constraints and will implicitly add a
constraint when one is missing. However, unlike grid layout table layout won't implicitly add a row if the
row/column count is incorrect
E.g this creates a 2x2 table but adds 5 elements. The 5th element won't show:
Table layout supports the ability to grow the last column which can be enabled using the
setGrowHorizontally
method. You can also use a shortened terse syntax to construct a table
layout however since the table layout is a constraint based layout you won't be able to utilize its full power
with this syntax.>
The default usage of the encloseIn below uses the setGrowHorizontally
flag.
To truly appreciate the TableLayout
we need to use the constraint syntax which allows
us to span, align and set width/height for the rows & columns.
Table layout works with a TableLayout.Constraint
instance that can communicate our intentions into the
layout manager. Such constraints can include more than one attribute e.g. span and height.
Notice that table layout constraints can't be reused for more than one component.
The constraint class supports the following attributes:
column | The column for the table cell. This defaults to -1 which will just place the component in the next available cell |
row | Similar to column, defaults to -1 as well |
width | The column width in percentages, -1 will use the preferred size. -2 for width will take up the rest of the available space |
height | The row height in percentages, -1 will use the preferred size. -2 for height will take up the rest of the available space |
spanHorizontal | The cells that should be occupied horizontally defaults to 1 and can't exceed the column count - current offset. |
spanVertical | Similar to spanHorizontal with the same limitations |
horizontalAlign | The horizontal alignment of the content within the cell, defaults to the special case -1 value to take up all the cell space can be either `-1`, `Component.LEFT`, `Component.RIGHT` or `Component.CENTER` |
verticalAlign | Similar to horizontalAlign can be one of `-1`, `Component.TOP`, `Component.BOTTOM` or `Component.CENTER` |
Notice that you only need to set `width`/`height` to one cell in a column/row.
The table layout constraint sample tries to demonstrate some of the unique things you can do with constraints.
We go into further details on this in the developer guide
so check that out.
Modifier and Type | Class and Description |
---|---|
static class |
TableLayout.Constraint
Represents the layout constraint for an entry within the table indicating
the desired position/behavior of the component.
|
Constructor and Description |
---|
TableLayout(int rows,
int columns)
A table must declare the amount of rows and columns in advance
|
Modifier and Type | Method and Description |
---|---|
void |
addLayoutComponent(Object value,
Component comp,
Container c)
Some layouts can optionally track the addition of elements with meta-data
that allows the user to "hint" on object positioning.
|
TableLayout.Constraint |
cc()
Creates a new Constraint instance to add to the layout, same as
createConstraint only shorter syntax |
TableLayout.Constraint |
cc(int row,
int column)
Creates a new Constraint instance to add to the layout, same as
createConstraint only shorter syntax |
TableLayout.Constraint |
createConstraint()
Creates a new Constraint instance to add to the layout
|
TableLayout.Constraint |
createConstraint(int row,
int column)
Creates a new Constraint instance to add to the layout
|
static Container |
encloseIn(int columns,
boolean growHorizontally,
Component... cmps)
Creates a table layout container, the number of rows is automatically calculated based on the number
of columns.
|
static Container |
encloseIn(int columns,
Component... cmps)
Creates a table layout container that grows the last column horizontally, the number of rows is automatically
calculated based on the number of columns.
|
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
int |
getCellHorizontalSpan(int row,
int column)
Returns the spanning for the table cell at the given coordinate
|
int |
getCellVerticalSpan(int row,
int column)
Returns the spanning for the table cell at the given coordinate
|
protected Component[] |
getChildrenInTraversalOrder(Container parent)
Gets the children of the parent container in the order that they should
be traversed when tabbing through a form.
|
int |
getColumnPosition(int col)
Returns the position of the given table column.
|
int |
getColumns()
Get the number of columns
|
Component |
getComponentAt(int row,
int column)
Returns the component at the given row/column
|
Object |
getComponentConstraint(Component comp)
Returns the optional component constraint
|
static int |
getDefaultColumnWidth()
Indicates the default (in percentage) for the column width, -1 indicates
automatic sizing
|
static int |
getDefaultRowHeight()
Indicates the default (in percentage) for the row height, -1 indicates
automatic sizing
|
static int |
getMinimumSizePerColumn()
Indicates the minimum size for a column in the table, this is applicable for tables that are
not scrollable on the X axis.
|
int |
getNextColumn()
Returns the column where the next operation of add will appear
|
int |
getNextRow()
Returns the row where the next operation of add will appear
|
Dimension |
getPreferredSize(Container parent)
Returns the container preferred size
|
int |
getRowPosition(int row)
Returns the position of the given table row.
|
int |
getRows()
Get the number of rows
|
boolean |
hasHorizontalSpanning()
Indicates whether there is spanning within this layout
|
boolean |
hasVerticalSpanning()
Indicates whether there is spanning within this layout
|
boolean |
isCellSpannedThroughHorizontally(int row,
int column)
Returns true if the cell at the given position is spanned through horizontally
|
boolean |
isCellSpannedThroughVertically(int row,
int column)
Returns true if the cell at the given position is spanned through vertically
|
boolean |
isConstraintTracking()
If this method returns true, the addLayoutComponent method will be called when replacing a
layout for every component within the container
|
boolean |
isGrowHorizontally()
Indicates whether the table layout should grow horizontally to take up available space by stretching the last column
|
boolean |
isTruncateHorizontally()
Indicates whether the table should be truncated if it do not have enough available horizontal space to display all its content.
|
boolean |
isTruncateVertically()
Indicates whether the table should be truncated if it do not have enough available vertical space to display all its content.
|
void |
layoutContainer(Container parent)
Layout the given parent container children
|
boolean |
overridesTabIndices(Container parent)
If a layout specifies a different traversal order of its components than the
component index, then it should override this method to return true, and
it should also override
Layout.getChildrenInTraversalOrder(com.codename1.ui.Container)
to set the tab indices of a container's children. |
void |
removeLayoutComponent(Component comp)
Removes the component from the layout this operation is only useful if the
layout maintains references to components within it
|
static void |
setDefaultColumnWidth(int w)
Indicates the default (in percentage) for the column width, -1 indicates
automatic sizing
|
static void |
setDefaultRowHeight(int h)
Indicates the default (in percentage) for the row height, -1 indicates
automatic sizing
|
void |
setGrowHorizontally(boolean growHorizontally)
Indicates whether the table layout should grow horizontally to take up available space by stretching the last column
|
static void |
setMinimumSizePerColumn(int minimumSize)
Sets the minimum size for a column in the table, this is applicable for tables that are
not scrollable on the X axis.
|
void |
setTruncateHorizontally(boolean truncateHorizontally)
Indicates whether the table should be truncated if it do not have enough available horizontal space to display all its content.
|
void |
setTruncateVertically(boolean truncateVertically)
Indicates whether the table should be truncated if it do not have enough available vertical space to display all its content.
|
String |
toString()
Returns a string representation of the object.
|
cloneConstraint, hashCode, isOverlapSupported, obscuresPotential, updateTabIndices
public TableLayout(int rows, int columns)
rows
- rows of the tablecolumns
- columns of the tablepublic int getRows()
public int getColumns()
public Component getComponentAt(int row, int column)
row
- the row of the componentcolumn
- the column of the componentpublic void layoutContainer(Container parent)
layoutContainer
in class Layout
parent
- the given parent containerpublic int getRowPosition(int row)
row
- the row in the tablepublic int getColumnPosition(int col)
col
- the column in the tablepublic Dimension getPreferredSize(Container parent)
getPreferredSize
in class Layout
parent
- the parent containerpublic int getNextRow()
public int getNextColumn()
public void addLayoutComponent(Object value, Component comp, Container c)
addLayoutComponent
in class Layout
value
- optional meta data information, like alignment orientationcomp
- the added component to the layoutc
- the parent containerpublic int getCellHorizontalSpan(int row, int column)
row
- row in the tablecolumn
- column within the tablepublic int getCellVerticalSpan(int row, int column)
row
- row in the tablecolumn
- column within the tablepublic boolean isCellSpannedThroughVertically(int row, int column)
row
- cell rowcolumn
- cell columnpublic boolean isCellSpannedThroughHorizontally(int row, int column)
row
- cell rowcolumn
- cell columnpublic boolean hasVerticalSpanning()
public boolean hasHorizontalSpanning()
public void removeLayoutComponent(Component comp)
removeLayoutComponent
in class Layout
comp
- the removed component from layoutpublic Object getComponentConstraint(Component comp)
getComponentConstraint
in class Layout
comp
- the component whose constraint should be returnedpublic TableLayout.Constraint createConstraint()
public TableLayout.Constraint cc()
createConstraint
only shorter syntaxpublic TableLayout.Constraint cc(int row, int column)
createConstraint
only shorter syntaxrow
- the row for the table starting with 0column
- the column for the table starting with 0public TableLayout.Constraint createConstraint(int row, int column)
row
- the row for the table starting with 0column
- the column for the table starting with 0public static void setMinimumSizePerColumn(int minimumSize)
minimumSize
- the minimum width of the columnpublic static int getMinimumSizePerColumn()
public static void setDefaultColumnWidth(int w)
w
- width in percentagepublic static int getDefaultColumnWidth()
public static void setDefaultRowHeight(int h)
h
- height in percentagepublic static int getDefaultRowHeight()
public String toString()
public boolean equals(Object o)
public boolean isConstraintTracking()
isConstraintTracking
in class Layout
public boolean isGrowHorizontally()
public void setGrowHorizontally(boolean growHorizontally)
growHorizontally
- the growHorizontally to setpublic boolean isTruncateHorizontally()
public void setTruncateHorizontally(boolean truncateHorizontally)
truncateHorizontally
- the truncateHorizontally to setpublic boolean isTruncateVertically()
public void setTruncateVertically(boolean truncateVertically)
truncateVertically
- the truncateVertically to setpublic static Container encloseIn(int columns, Component... cmps)
Creates a table layout container that grows the last column horizontally, the number of rows is automatically calculated based on the number of columns. See usage:
columns
- the number of columnscmps
- components to addpublic static Container encloseIn(int columns, boolean growHorizontally, Component... cmps)
Creates a table layout container, the number of rows is automatically calculated based on the number of columns. See usage:
columns
- the number of columnsgrowHorizontally
- true to grow the last column to fit available widthcmps
- components to addpublic boolean overridesTabIndices(Container parent)
Layout
Layout.getChildrenInTraversalOrder(com.codename1.ui.Container)
to set the tab indices of a container's children.overridesTabIndices
in class Layout
parent
- The parent component.protected Component[] getChildrenInTraversalOrder(Container parent)
Layout
This should only be overridden if the Layout defines a different traversal order than the standard index order.
Layouts that implement this method, should override the Layout.overridesTabIndices(com.codename1.ui.Container)
method to return true.
getChildrenInTraversalOrder
in class Layout