public class FlowLayout
FlowLayout is the default layout manager for Codename One Containers and Forms. It places components in a row one after another based on their preferred size. When it reaches the edge of the container it will break a line and start a new row.
Form hi = new Form("Flow Layout", new FlowLayout());
hi.add(new Label("First")).
add(new Label("Second")).
add(new Label("Third")).
add(new Label("Fourth")).
add(new Label("Fifth"));
hi.show();
Since flow layout isn’t a constraint based layout it has a bunch of very useful enclose methods that can significantly reduce the code required to create the same UI e.g.:
Container flowLayout = FlowLayout.encloseIn(new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth"));
This class works nicely for simple elements, however since Codename One doesn’t reflow recursively (for performance) it can’t accurately handle complex layouts. As a result when an element of varying size is placed in a flow layout this confuses the line breaking logic and fails in odd ways. That is why this layout should only be used for relatively simple use cases.
Flow layout supports aligning the component horizontally and vertically, it defaults to the top left alignment for
LTR languages. E.g. the following alignments are supported thru the usage of setAlign &
setValign.
E.g. you can align to the center
You can align to the right
You can align to the center and the middle horizontally
There are quite a few additional combinations that are possible with these API’s.
Constructors
public FlowLayout() | Creates a new instance of FlowLayout with left alignment |
public FlowLayout(int orientation) | Creates a new instance of FlowLayout with the given orientation one of LEFT, RIGHT or CENTER |
public FlowLayout(int orientation, int valign) | Creates a new instance of FlowLayout with the given orientation one of LEFT, RIGHT or CENTER and the vertical orientation |
public FlowLayout(int orientation, int valign, boolean vAlignByRow) | Creates a new instance of FlowLayout with the given orientation one of LEFT, RIGHT or CENTER and the vertical orientation |
Methods
public static Container encloseIn(Component... cmps) | Shorthand for com.codename1.ui.Component...) with a FlowLayout instance see: |
public static Container encloseCenter(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER), cmps); |
public static Container encloseRight(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT), cmps); |
public static Container encloseMiddle(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps); |
public static Container encloseMiddleByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps); |
public static Container encloseCenterMiddle(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER), cmps); |
public static Container encloseCenterMiddleByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER, true), cmps); |
public static Container encloseRightMiddle(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER), cmps); |
public static Container encloseRightMiddleByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER, true), cmps); |
public static Container encloseLeftMiddle(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps); |
public static Container encloseLeftMiddleByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps); |
public static Container encloseBottom(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM), cmps); |
public static Container encloseCenterBottom(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM), cmps); |
public static Container encloseRightBottom(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM), cmps); |
public static Container encloseBottomByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM, true), cmps); |
public static Container encloseCenterBottomByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM, true), cmps); |
public static Container encloseRightBottomByRow(Component... cmps) | Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM, true), cmps); |
public void layoutContainer(Container parent) | Layout the given parent container children |
protected void fillRow(Container target, int width, int start, int end) | This method tries to fill up the available space in a row. |
public Dimension getPreferredSize(Container parent) | Returns the container preferred size |
public String toString() | Returns a string representation of the object. |
public boolean isFillRows() | Indicates whether the layout manager should try to fill up the available space in the row |
public void setFillRows(boolean fillRows) | Indicates whether the layout manager should try to fill up the available space in the row |
public int getValign() | Indicates vertical alignment within the flow layout |
public void setValign(int valign) | Indicates vertical alignment within the flow layout |
public boolean isValignByRow() | Returns whether vertical alignment is done internally or externally |
public void setValignByRow(boolean internal) | When set to true vertical alignment will be performed by row (components within the container will be aligned vertically to each other in the same row) When set to false (which is default) vertical alignment relates to the alignment of this… |
public int getAlign() | Alignment of the flow layout, defaults to LEFT |
public void setAlign(int orientation) | Alignment of the flow layout, defaults to LEFT |
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. |
Inherited methods
Constructor details
FlowLayout
public FlowLayout()FlowLayout
public FlowLayout(int orientation)Parameters
orientationint- the orientation value
FlowLayout
public FlowLayout(int orientation, int valign)Parameters
orientationint- the orientation value
valignint- the vertical orientation one of Component.TOP/BOTTOM/CENTER
FlowLayout
public FlowLayout(int orientation, int valign, boolean vAlignByRow)Parameters
orientationint- the orientation value
valignint- the vertical orientation one of Component.TOP/BOTTOM/CENTER
vAlignByRowboolean- whether vertical alignment should be computed by row elements
Method details
encloseIn
public static Container encloseIn(Component... cmps)Shorthand for com.codename1.ui.Component...)
with a FlowLayout instance see:
Container flowLayout = FlowLayout.encloseIn(new Label("First"),
new Label("Second"),
new Label("Third"),
new Label("Fourth"),
new Label("Fifth"));
Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseCenter
public static Container encloseCenter(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseRight
public static Container encloseRight(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseMiddle
public static Container encloseMiddle(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseMiddleByRow
public static Container encloseMiddleByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseCenterMiddle
public static Container encloseCenterMiddle(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseCenterMiddleByRow
public static Container encloseCenterMiddleByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseRightMiddle
public static Container encloseRightMiddle(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseRightMiddleByRow
public static Container encloseRightMiddleByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseLeftMiddle
public static Container encloseLeftMiddle(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseLeftMiddleByRow
public static Container encloseLeftMiddleByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseBottom
public static Container encloseBottom(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseCenterBottom
public static Container encloseCenterBottom(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseRightBottom
public static Container encloseRightBottom(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseBottomByRow
public static Container encloseBottomByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseCenterBottomByRow
public static Container encloseCenterBottomByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
encloseRightBottomByRow
public static Container encloseRightBottomByRow(Component... cmps)Parameters
cmpsComponent...- the components to enclose in a new container
Returns
layoutContainer
public void layoutContainer(Container parent)Parameters
parentContainer- the given parent container
fillRow
protected void fillRow(Container target, int width, int start, int end)Parameters
targetContainer- the parent container
widthint- the width of the row to fill
startint- the index of the first component in this row
endint- the index of the last component in this row
getPreferredSize
public Dimension getPreferredSize(Container parent)Parameters
parentContainer- the parent container
Returns
toString
public String toString()isFillRows
public boolean isFillRows()Returns
setFillRows
public void setFillRows(boolean fillRows)Parameters
fillRowsboolean- the fillRows to set
getValign
public int getValign()Returns
setValign
public void setValign(int valign)Parameters
valignint- one of Component.TOP/BOTTOM/CENTER
isValignByRow
public boolean isValignByRow()Returns
setValignByRow
public void setValignByRow(boolean internal)Parameters
internalboolean- true for internal, false otherwise
getAlign
public int getAlign()Returns
setAlign
public void setAlign(int orientation)Parameters
orientationint- the orientation to set
equals
public boolean equals(Object o)hashCode
public int hashCode()