public class TableLayout
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:
Form hi = new Form("Table Layout 2x2", new TableLayout(2, 2));
hi.add(new Label("First")).
add(new Label("Second")).
add(new Label("Third")).
add(new Label("Fourth")).
add(new Label("Fifth"));
hi.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.
Container tl = TableLayout.encloseIn(2, new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth"));
The Full Potential
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 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.
TableLayout tl = new TableLayout(2, 3);
Form hi = new Form("Table Layout Cons", tl);
hi.setScrollable(false);
hi.add(tl.createConstraint().
widthPercentage(20),
new Label("AAA")).
add(tl.createConstraint().
horizontalSpan(2).
heightPercentage(80).
verticalAlign(Component.CENTER).
horizontalAlign(Component.CENTER),
new Label("Span H")).
add(new Label("BBB")).
add(tl.createConstraint().
widthPercentage(60).
heightPercentage(20),
new Label("CCC")).
add(tl.createConstraint().
widthPercentage(20),
new Label("DDD"));
Nested types
class TableLayout.Constraint | Represents the layout constraint for an entry within the table indicating the desired position/behavior of the component. |
Constructors
public TableLayout(int rows, int columns) | A table must declare the amount of rows and columns in advance |
Methods
public 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. |
public 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. |
public static int getDefaultColumnWidth() | Indicates the default (in percentage) for the column width, -1 indicates automatic sizing |
public static void setDefaultColumnWidth(int w) | Indicates the default (in percentage) for the column width, -1 indicates automatic sizing |
public static int getDefaultRowHeight() | Indicates the default (in percentage) for the row height, -1 indicates automatic sizing |
public static void setDefaultRowHeight(int h) | Indicates the default (in percentage) for the row height, -1 indicates automatic sizing |
public 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. |
public 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. |
public int getRows() | Get the number of rows |
public int getColumns() | Get the number of columns |
public Component getComponentAt(int row, int column) | Returns the component at the given row/column |
public void layoutContainer(Container parent) | Layout the given parent container children |
public int getRowPosition(int row) | Returns the position of the given table row. |
public int getColumnPosition(int col) | Returns the position of the given table column. |
public Dimension getPreferredSize(Container parent) | Returns the container preferred size |
public int getNextRow() | Returns the row where the next operation of add will appear |
public int getNextColumn() | Returns the column where the next operation of add will appear |
public 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. |
public int getCellHorizontalSpan(int row, int column) | Returns the spanning for the table cell at the given coordinate |
public int getCellVerticalSpan(int row, int column) | Returns the spanning for the table cell at the given coordinate |
public boolean isCellSpannedThroughVertically(int row, int column) | Returns true if the cell at the given position is spanned through vertically |
public boolean isCellSpannedThroughHorizontally(int row, int column) | Returns true if the cell at the given position is spanned through horizontally |
public boolean hasVerticalSpanning() | Indicates whether there is spanning within this layout |
public boolean hasHorizontalSpanning() | Indicates whether there is spanning within this layout |
public void removeLayoutComponent(Component comp) | Removes the component from the layout this operation is only useful if the layout maintains references to components within it |
public Object getComponentConstraint(Component comp) | Returns the optional component constraint |
public TableLayout.Constraint createConstraint() | Creates a new Constraint instance to add to the layout |
public TableLayout.Constraint cc() | Creates a new Constraint instance to add to the layout, same as createConstraint only shorter syntax |
public TableLayout.Constraint cc(int row, int column) | Creates a new Constraint instance to add to the layout, same as createConstraint only shorter syntax |
public TableLayout.Constraint createConstraint(int row, int column) | Creates a new Constraint instance to add to the layout |
public String toString() | Returns a string representation of the object. |
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 isConstraintTracking() | If this method returns true, the addLayoutComponent method will be called when replacing a layout for every component within the container |
public boolean isGrowHorizontally() | Indicates whether the table layout should grow horizontally to take up available space by stretching the last column |
public void setGrowHorizontally(boolean growHorizontally) | Indicates whether the table layout should grow horizontally to take up available space by stretching the last column |
public boolean isTruncateHorizontally() | Indicates whether the table should be truncated if it do not have enough available horizontal space to display all its content. |
public 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. |
public boolean isTruncateVertically() | Indicates whether the table should be truncated if it do not have enough available vertical space to display all its content. |
public 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. |
public 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 #getChildrenInTraversalOrder(com.codename1.ui.Container) to set the tab indices of a container’s children. |
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. |
Inherited methods
Constructor details
TableLayout
public TableLayout(int rows, int columns)Parameters
rowsint- rows of the table
columnsint- columns of the table
Method details
getMinimumSizePerColumn
public static int getMinimumSizePerColumn()Returns
setMinimumSizePerColumn
public static void setMinimumSizePerColumn(int minimumSize)Parameters
minimumSizeint- the minimum width of the column
getDefaultColumnWidth
public static int getDefaultColumnWidth()Returns
setDefaultColumnWidth
public static void setDefaultColumnWidth(int w)Parameters
wint- width in percentage
getDefaultRowHeight
public static int getDefaultRowHeight()Returns
setDefaultRowHeight
public static void setDefaultRowHeight(int h)Parameters
hint- height in percentage
encloseIn
public 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:
Container tl = TableLayout.encloseIn(2, new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth"));
Parameters
columnsint- the number of columns
cmpsComponent...- components to add
Returns
encloseIn
public 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:
Container tl = TableLayout.encloseIn(2, new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth"));
Parameters
columnsint- the number of columns
growHorizontallyboolean- true to grow the last column to fit available width
cmpsComponent...- components to add
Returns
getRows
public int getRows()Returns
getColumns
public int getColumns()Returns
getComponentAt
public Component getComponentAt(int row, int column)Parameters
rowint- the row of the component
columnint- the column of the component
Returns
layoutContainer
public void layoutContainer(Container parent)Parameters
parentContainer- the given parent container
getRowPosition
public int getRowPosition(int row)Parameters
rowint- the row in the table
Returns
getColumnPosition
public int getColumnPosition(int col)Parameters
colint- the column in the table
Returns
getPreferredSize
public Dimension getPreferredSize(Container parent)Parameters
parentContainer- the parent container
Returns
getNextRow
public int getNextRow()Returns
getNextColumn
public int getNextColumn()Returns
addLayoutComponent
public void addLayoutComponent(Object value, Component comp, Container c)Parameters
valueObject- optional meta data information, like alignment orientation
compComponent- the added component to the layout
cContainer- the parent container
getCellHorizontalSpan
public int getCellHorizontalSpan(int row, int column)Parameters
rowint- row in the table
columnint- column within the table
Returns
getCellVerticalSpan
public int getCellVerticalSpan(int row, int column)Parameters
rowint- row in the table
columnint- column within the table
Returns
isCellSpannedThroughVertically
public boolean isCellSpannedThroughVertically(int row, int column)Parameters
rowint- cell row
columnint- cell column
Returns
isCellSpannedThroughHorizontally
public boolean isCellSpannedThroughHorizontally(int row, int column)Parameters
rowint- cell row
columnint- cell column
Returns
hasVerticalSpanning
public boolean hasVerticalSpanning()Returns
hasHorizontalSpanning
public boolean hasHorizontalSpanning()Returns
removeLayoutComponent
public void removeLayoutComponent(Component comp)Parameters
compComponent- the removed component from layout
getComponentConstraint
public Object getComponentConstraint(Component comp)Parameters
compComponent- the component whose constraint should be returned
Returns
createConstraint
public TableLayout.Constraint createConstraint()Returns
cc
public TableLayout.Constraint cc()createConstraint only shorter syntaxReturns
cc
public TableLayout.Constraint cc(int row, int column)createConstraint only shorter syntaxParameters
rowint- the row for the table starting with 0
columnint- the column for the table starting with 0
Returns
createConstraint
public TableLayout.Constraint createConstraint(int row, int column)Parameters
rowint- the row for the table starting with 0
columnint- the column for the table starting with 0
Returns
toString
public String toString()equals
public boolean equals(Object o)hashCode
public int hashCode()isConstraintTracking
public boolean isConstraintTracking()Returns
isGrowHorizontally
public boolean isGrowHorizontally()Returns
setGrowHorizontally
public void setGrowHorizontally(boolean growHorizontally)Parameters
growHorizontallyboolean- the growHorizontally to set
isTruncateHorizontally
public boolean isTruncateHorizontally()Returns
setTruncateHorizontally
public void setTruncateHorizontally(boolean truncateHorizontally)Parameters
truncateHorizontallyboolean- the truncateHorizontally to set
isTruncateVertically
public boolean isTruncateVertically()Returns
setTruncateVertically
public void setTruncateVertically(boolean truncateVertically)Parameters
truncateVerticallyboolean- the truncateVertically to set
overridesTabIndices
public boolean overridesTabIndices(Container parent)#getChildrenInTraversalOrder(com.codename1.ui.Container)
to set the tab indices of a container’s children.Parameters
parentContainer- The parent component.
Returns
getChildrenInTraversalOrder
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.
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 #overridesTabIndices(com.codename1.ui.Container)
method to return true.