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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents the layout constraint for an entry within the table indicating the desired position/behavior of the component. -
Constructor Summary
ConstructorsConstructorDescriptionTableLayout(int rows, int columns) A table must declare the amount of rows and columns in advance -
Method Summary
Modifier and TypeMethodDescriptionvoidaddLayoutComponent(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.cc()Creates a new Constraint instance to add to the layout, same ascreateConstraintonly shorter syntaxcc(int row, int column) Creates a new Constraint instance to add to the layout, same ascreateConstraintonly shorter syntaxCreates a new Constraint instance to add to the layoutcreateConstraint(int row, int column) Creates a new Constraint instance to add to the layoutstatic ContainerCreates a table layout container, the number of rows is automatically calculated based on the number of columns.static ContainerCreates a table layout container that grows the last column horizontally, the number of rows is automatically calculated based on the number of columns.booleanintgetCellHorizontalSpan(int row, int column) Returns the spanning for the table cell at the given coordinateintgetCellVerticalSpan(int row, int column) Returns the spanning for the table cell at the given coordinateprotected Component[]getChildrenInTraversalOrder(Container parent) Gets the children of the parent container in the order that they should be traversed when tabbing through a form.intgetColumnPosition(int col) Returns the position of the given table column.intGet the number of columnsgetComponentAt(int row, int column) Returns the component at the given row/columnReturns the optional component constraintstatic intIndicates the default (in percentage) for the column width, -1 indicates automatic sizingstatic intIndicates the default (in percentage) for the row height, -1 indicates automatic sizingstatic intIndicates the minimum size for a column in the table, this is applicable for tables that are not scrollable on the X axis.intReturns the column where the next operation of add will appearintReturns the row where the next operation of add will appeargetPreferredSize(Container parent) Returns the container preferred sizeintgetRowPosition(int row) Returns the position of the given table row.intgetRows()Get the number of rowsinthashCode()booleanIndicates whether there is spanning within this layoutbooleanIndicates whether there is spanning within this layoutbooleanisCellSpannedThroughHorizontally(int row, int column) Returns true if the cell at the given position is spanned through horizontallybooleanisCellSpannedThroughVertically(int row, int column) Returns true if the cell at the given position is spanned through verticallybooleanIf this method returns true, the addLayoutComponent method will be called when replacing a layout for every component within the containerbooleanIndicates whether the table layout should grow horizontally to take up available space by stretching the last columnbooleanIndicates whether the table should be truncated if it do not have enough available horizontal space to display all its content.booleanIndicates whether the table should be truncated if it do not have enough available vertical space to display all its content.voidlayoutContainer(Container parent) Layout the given parent container childrenbooleanoverridesTabIndices(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.voidRemoves the component from the layout this operation is only useful if the layout maintains references to components within itstatic voidsetDefaultColumnWidth(int w) Indicates the default (in percentage) for the column width, -1 indicates automatic sizingstatic voidsetDefaultRowHeight(int h) Indicates the default (in percentage) for the row height, -1 indicates automatic sizingvoidsetGrowHorizontally(boolean growHorizontally) Indicates whether the table layout should grow horizontally to take up available space by stretching the last columnstatic voidsetMinimumSizePerColumn(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.voidsetTruncateHorizontally(boolean truncateHorizontally) Indicates whether the table should be truncated if it do not have enough available horizontal space to display all its content.voidsetTruncateVertically(boolean truncateVertically) Indicates whether the table should be truncated if it do not have enough available vertical space to display all its content.toString()Methods inherited from class Layout
cloneConstraint, isOverlapSupported, obscuresPotential, updateTabIndices
-
Constructor Details
-
TableLayout
public TableLayout(int rows, int columns) A table must declare the amount of rows and columns in advance
Parameters
-
rows: rows of the table -
columns: columns of the table
-
-
-
Method Details
-
getMinimumSizePerColumn
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. This will force the earlier columns to leave room for the latter columns.
Returns
the minimum width of the column
-
setMinimumSizePerColumn
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. This will force the earlier columns to leave room for the latter columns.
Parameters
minimumSize: the minimum width of the column
-
getDefaultColumnWidth
public static int getDefaultColumnWidth()Indicates the default (in percentage) for the column width, -1 indicates automatic sizing
Returns
width in percentage
-
setDefaultColumnWidth
public static void setDefaultColumnWidth(int w) Indicates the default (in percentage) for the column width, -1 indicates automatic sizing
Parameters
w: width in percentage
-
getDefaultRowHeight
public static int getDefaultRowHeight()Indicates the default (in percentage) for the row height, -1 indicates automatic sizing
Returns
height in percentage
-
setDefaultRowHeight
public static void setDefaultRowHeight(int h) Indicates the default (in percentage) for the row height, -1 indicates automatic sizing
Parameters
h: height in percentage
-
encloseIn
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
-
columns: the number of columns -
cmps: components to add
Returns
a newly created table layout container with the components in it
-
-
encloseIn
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
-
columns: the number of columns -
growHorizontally: true to grow the last column to fit available width -
cmps: components to add
Returns
a newly created table layout container with the components in it
-
-
getRows
public int getRows()Get the number of rows
Returns
number of rows
-
getColumns
public int getColumns()Get the number of columns
Returns
number of columns
-
getComponentAt
Returns the component at the given row/column
Parameters
-
row: the row of the component -
column: the column of the component
Returns
the component instance
-
-
layoutContainer
Layout the given parent container children
Parameters
parent: the given parent container
- Specified by:
layoutContainerin classLayout
-
getRowPosition
public int getRowPosition(int row) Returns the position of the given table row. A valid value is only returned after the layout occurred.
Parameters
row: the row in the table
Returns
the Y position in pixels or -1 if layout hasn't occured/row is too large etc.
-
getColumnPosition
public int getColumnPosition(int col) Returns the position of the given table column. A valid value is only returned after the layout occurred.
Parameters
col: the column in the table
Returns
the X position in pixels or -1 if layout hasn't occured/column is too large etc.
-
getPreferredSize
Returns the container preferred size
Parameters
parent: the parent container
Returns
the container preferred size
- Specified by:
getPreferredSizein classLayout
-
getNextRow
public int getNextRow()Returns the row where the next operation of add will appear
Returns
the row where the next operation of add will appear
-
getNextColumn
public int getNextColumn()Returns the column where the next operation of add will appear
Returns
the column where the next operation of add will appear
-
addLayoutComponent
Some layouts can optionally track the addition of elements with meta-data that allows the user to "hint" on object positioning.
Parameters
-
value: optional meta data information, like alignment orientation -
comp: the added component to the layout -
c: the parent container
- Overrides:
addLayoutComponentin classLayout
-
-
getCellHorizontalSpan
public int getCellHorizontalSpan(int row, int column) Returns the spanning for the table cell at the given coordinate
Parameters
-
row: row in the table -
column: column within the table
Returns
the amount of spanning 1 for no spanning
-
-
getCellVerticalSpan
public int getCellVerticalSpan(int row, int column) Returns the spanning for the table cell at the given coordinate
Parameters
-
row: row in the table -
column: column within the table
Returns
the amount of spanning 1 for no spanning
-
-
isCellSpannedThroughVertically
public boolean isCellSpannedThroughVertically(int row, int column) Returns true if the cell at the given position is spanned through vertically
Parameters
-
row: cell row -
column: cell column
Returns
true if the cell is a part of a span for another cell
-
-
isCellSpannedThroughHorizontally
public boolean isCellSpannedThroughHorizontally(int row, int column) Returns true if the cell at the given position is spanned through horizontally
Parameters
-
row: cell row -
column: cell column
Returns
true if the cell is a part of a span for another cell
-
-
hasVerticalSpanning
public boolean hasVerticalSpanning()Indicates whether there is spanning within this layout
Returns
true if the layout makes use of spanning
-
hasHorizontalSpanning
public boolean hasHorizontalSpanning()Indicates whether there is spanning within this layout
Returns
true if the layout makes use of spanning
-
removeLayoutComponent
Removes the component from the layout this operation is only useful if the layout maintains references to components within it
Parameters
comp: the removed component from layout
- Overrides:
removeLayoutComponentin classLayout
-
getComponentConstraint
Returns the optional component constraint
Parameters
comp: the component whose constraint should be returned
Returns
the optional component constraint
- Overrides:
getComponentConstraintin classLayout
-
createConstraint
Creates a new Constraint instance to add to the layout
Returns
the default constraint
-
cc
Creates a new Constraint instance to add to the layout, same as
createConstraintonly shorter syntaxReturns
the default constraint
-
cc
Creates a new Constraint instance to add to the layout, same as
createConstraintonly shorter syntaxParameters
-
row: the row for the table starting with 0 -
column: the column for the table starting with 0
Returns
the new constraint
-
-
createConstraint
Creates a new Constraint instance to add to the layout
Parameters
-
row: the row for the table starting with 0 -
column: the column for the table starting with 0
Returns
the new constraint
-
-
toString
-
equals
-
hashCode
-
isConstraintTracking
public boolean isConstraintTracking()If this method returns true, the addLayoutComponent method will be called when replacing a layout for every component within the container
Returns
false by default
- Overrides:
isConstraintTrackingin classLayout
-
isGrowHorizontally
public boolean isGrowHorizontally()Indicates whether the table layout should grow horizontally to take up available space by stretching the last column
Returns
the growHorizontally
-
setGrowHorizontally
public void setGrowHorizontally(boolean growHorizontally) Indicates whether the table layout should grow horizontally to take up available space by stretching the last column
Parameters
growHorizontally: the growHorizontally to set
-
isTruncateHorizontally
public boolean isTruncateHorizontally()Indicates whether the table should be truncated if it do not have enough available horizontal space to display all its content. If not, will shrink
Returns
the truncateHorizontally
-
setTruncateHorizontally
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. If not, will shrink
Parameters
truncateHorizontally: the truncateHorizontally to set
-
isTruncateVertically
public boolean isTruncateVertically()Indicates whether the table should be truncated if it do not have enough available vertical space to display all its content. If not, will shrink
Returns
the truncateVertically
-
setTruncateVertically
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. If not, will shrink
Parameters
truncateVertically: the truncateVertically to set
-
overridesTabIndices
Description copied from class:LayoutIf 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.Parameters
parent: The parent component.
Returns
True if this layout overrides tab traversal order.
- Overrides:
overridesTabIndicesin classLayout
-
getChildrenInTraversalOrder
Description copied from class:LayoutGets 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.Parameters
parent
Returns
Array of Components in the order
- Overrides:
getChildrenInTraversalOrderin classLayout
-