# Class FlowLayout

java.lang.Object
com.codename1.ui.layouts.Layout
com.codename1.ui.layouts.FlowLayout

public class FlowLayout extends Layout

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.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FlowLayout()`
  Creates a new instance of FlowLayout with left alignment
  `FlowLayout(int orientation)`
  Creates a new instance of FlowLayout with the given orientation one of LEFT, RIGHT or CENTER
  `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
  `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

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Container`
  `encloseBottom(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM), cmps);
  `static Container`
  `encloseBottomByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM, true), cmps);
  `static Container`
  `encloseCenter(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER), cmps);
  `static Container`
  `encloseCenterBottom(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM), cmps);
  `static Container`
  `encloseCenterBottomByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM, true), cmps);
  `static Container`
  `encloseCenterMiddle(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER), cmps);
  `static Container`
  `encloseCenterMiddleByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER, true), cmps);
  `static Container`
  `encloseIn(Component... cmps)`
  Shorthand for `com.codename1.ui.Component...)` with a `FlowLayout instance` see:
  `static Container`
  `encloseLeftMiddle(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps);
  `static Container`
  `encloseLeftMiddleByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps);
  `static Container`
  `encloseMiddle(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps);
  `static Container`
  `encloseMiddleByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps);
  `static Container`
  `encloseRight(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT), cmps);
  `static Container`
  `encloseRightBottom(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM), cmps);
  `static Container`
  `encloseRightBottomByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM, true), cmps);
  `static Container`
  `encloseRightMiddle(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER), cmps);
  `static Container`
  `encloseRightMiddleByRow(Component... cmps)`
  Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER, true), cmps);
  `boolean`
  `equals(Object o)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `fillRow(Container target, int width, int start, int end)`
  This method tries to fill up the available space in a row.
  `int`
  `getAlign()`
  Alignment of the flow layout, defaults to LEFT
  `Dimension`
  `getPreferredSize(Container parent)`
  Returns the container preferred size
  `int`
  `getValign()`
  Indicates vertical alignment within the flow layout
  `int`
  `hashCode()`
  Returns a hash code value for the object.
  `boolean`
  `isFillRows()`
  Indicates whether the layout manager should try to fill up the available space in the row
  `boolean`
  `isValignByRow()`
  Returns whether vertical alignment is done internally or externally
  `void`
  `layoutContainer(Container parent)`
  Layout the given parent container children
  `void`
  `setAlign(int orientation)`
  Alignment of the flow layout, defaults to LEFT
  `void`
  `setFillRows(boolean fillRows)`
  Indicates whether the layout manager should try to fill up the available space in the row
  `void`
  `setValign(int valign)`
  Indicates vertical alignment within the flow layout
  `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 container in regards to external components
  `String`
  `toString()`
  Returns a string representation of the object.

  ### Methods inherited from class Layout

  `addLayoutComponent, cloneConstraint, getChildrenInTraversalOrder, getComponentConstraint, isConstraintTracking, isOverlapSupported, obscuresPotential, overridesTabIndices, removeLayoutComponent, updateTabIndices`

  ### Methods inherited from class Object

  `clone, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### FlowLayout

    public FlowLayout()
    Creates a new instance of FlowLayout with left alignment

  - ### FlowLayout

    public FlowLayout(int orientation)

    Creates a new instance of FlowLayout with the given orientation one of LEFT, RIGHT or CENTER

    ###### Parameters

    - `orientation`: the orientation value

  - ### FlowLayout

    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

    ###### Parameters

    - `orientation`: the orientation value

    - `valign`: the vertical orientation one of Component.TOP/BOTTOM/CENTER

  - ### FlowLayout

    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

    ###### Parameters

    - `orientation`: the orientation value

    - `valign`: the vertical orientation one of Component.TOP/BOTTOM/CENTER

    - `vAlignByRow`: 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

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseCenter

    public static Container encloseCenter(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseRight

    public static Container encloseRight(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseMiddle

    public static Container encloseMiddle(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseMiddleByRow

    public static Container encloseMiddleByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseCenterMiddle

    public static Container encloseCenterMiddle(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseCenterMiddleByRow

    public static Container encloseCenterMiddleByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.CENTER, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseRightMiddle

    public static Container encloseRightMiddle(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseRightMiddleByRow

    public static Container encloseRightMiddleByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.CENTER, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseLeftMiddle

    public static Container encloseLeftMiddle(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseLeftMiddleByRow

    public static Container encloseLeftMiddleByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.CENTER, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseBottom

    public static Container encloseBottom(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseCenterBottom

    public static Container encloseCenterBottom(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseRightBottom

    public static Container encloseRightBottom(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseBottomByRow

    public static Container encloseBottomByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.LEFT, Component.BOTTOM, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseCenterBottomByRow

    public static Container encloseCenterBottomByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.CENTER, Component.BOTTOM, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### encloseRightBottomByRow

    public static Container encloseRightBottomByRow(Component... cmps)

    Shorthand for Container.encloseIn(new FlowLayout(Component.RIGHT, Component.BOTTOM, true), cmps);

    ###### Parameters

    - `cmps`: the components to enclose in a new container

    ###### Returns

    the new container

  - ### layoutContainer

    public void layoutContainer(Container parent)

    Layout the given parent container children

    ###### Parameters

    - `parent`: the given parent container
    Specified by:
      `layoutContainer` in class `Layout`

  - ### fillRow

    protected void fillRow(Container target, int width, int start, int end)

    This method tries to fill up the available space in a row. This method is called if isFillRows() returns true.

    ###### Parameters

    - `target`: the parent container

    - `width`: the width of the row to fill

    - `start`: the index of the first component in this row

    - `end`: the index of the last component in this row

  - ### getPreferredSize

    public Dimension getPreferredSize(Container parent)

    Returns the container preferred size

    ###### Parameters

    - `parent`: the parent container

    ###### Returns

    the container preferred size

    Specified by:
      `getPreferredSize` in class `Layout`

  - ### toString

    public String toString()
    Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method. The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())
    Overrides:
      `toString` in class `Object`

  - ### isFillRows

    public boolean isFillRows()

    Indicates whether the layout manager should try to fill up the available space in the row

    ###### Returns

    the fillRows

  - ### setFillRows

    public void setFillRows(boolean fillRows)

    Indicates whether the layout manager should try to fill up the available space in the row

    ###### Parameters

    - `fillRows`: the fillRows to set

  - ### getValign

    public int getValign()

    Indicates vertical alignment within the flow layout

    ###### Returns

    Component.TOP/BOTTOM/CENTER

  - ### setValign

    public void setValign(int valign)

    Indicates vertical alignment within the flow layout

    ###### Parameters

    - `valign`: one of Component.TOP/BOTTOM/CENTER

  - ### isValignByRow

    public boolean isValignByRow()

    Returns whether vertical alignment is done internally or externally

    ###### Returns

    whether vertical alignment is done internally or externally

  - ### setValignByRow

    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 container in regards to external components

    ###### Parameters

    - `internal`: true for internal, false otherwise

  - ### getAlign

    public int getAlign()

    Alignment of the flow layout, defaults to LEFT

    ###### Returns

    the orientation

  - ### setAlign

    public void setAlign(int orientation)

    Alignment of the flow layout, defaults to LEFT

    ###### Parameters

    - `orientation`: the orientation to set

  - ### equals

    public boolean equals(Object o)
    Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
    Overrides:
      `equals` in class `Layout`

  - ### hashCode

    public int hashCode()
    Description copied from class: `Layout`
    Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable. The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables. As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
    Overrides:
      `hashCode` in class `Layout`