# Class BoxLayout

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

public class BoxLayout extends Layout

Layout manager that places elements in a row (`X_AXIS`) or column (`Y_AXIS`) according to box orientation. Box is a very simple and predictable layout that serves as the "workhorse" of component lists in Codename One

You can create a box layout Y UI using syntax such as this

    Form hi = new Form("Box Y Layout", new BoxLayout(BoxLayout.Y_AXIS));
    hi.add(new Label("First")).
        add(new Label("Second")).
        add(new Label("Third")).
        add(new Label("Fourth")).
        add(new Label("Fifth"));

This can also be expressed with more terse syntax e.g. an X axis layout like this:

    Container box = BoxLayout.encloseX(new Label("First"),
            new Label("Second"),
            new Label("Third"),
            new Label("Fourth"),
            new Label("Fifth"));

The `BoxLayout` keeps the preferred size of its destination orientation and scales elements on the other axis. Specifically `X_AXIS` will keep the preferred width of the component while growing all the components vertically to match in size. Its `Y_AXIS` counterpart keeps the preferred height while growing the components horizontally.

This behavior is very useful since it allows elements to align as they would all have the same size.

In some cases the growing behavior in the X axis is undesired, for these cases we can use the `X_AXIS_NO_GROW` variant.

FlowLayout vs. BoxLayout.X_AXIS/X_AXIS_NO_GROW

There are quite a few differences between `FlowLayout` and `BoxLayout`. When it doesn't matter to you we tend to recommend `BoxLayout` as it acts more consistently in all situations since its far simpler. Another advantage of `BoxLayout` is the fact that it grows and thus aligns nicely.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `X_AXIS`
  Horizontal layout where components are arranged from left to right
  `static final int`
  `X_AXIS_NO_GROW`
  Horizontal layout where components are arranged from left to right but don't grow vertically beyond their preferred size
  `static final int`
  `Y_AXIS`
  Vertical layout where components are arranged from top to bottom
  `static final int`
  `Y_AXIS_BOTTOM_LAST`
  Same as Y_AXIS with a special case for the last component.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BoxLayout(int axis)`
  Creates a new instance of BoxLayout

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static Container`
  `encloseX(Component... cmps)`
  The equivalent of Container.enclose() with a box layout X
  `static Container`
  `encloseXCenter(Component... cmps)`
  The equivalent of Container.enclose() with a box layout X, with center alignment.
  `static Container`
  `encloseXNoGrow(Component... cmps)`
  The equivalent of Container.enclose() with a box layout X no grow option
  `static Container`
  `encloseXRight(Component... cmps)`
  The equivalent of Container.enclose() with a box layout X, with right alignment.
  `static Container`
  `encloseY(Component... cmps)`
  The equivalent of Container.enclose() with a box layout Y
  `static Container`
  `encloseYBottom(Component... cmps)`
  The equivalent of Container.enclose() with a box layout Y, with bottom alignment.
  `static Container`
  `encloseYBottomLast(Component... cmps)`
  The equivalent of Container.enclose() with a box layout Y in bottom last mode
  `static Container`
  `encloseYCenter(Component... cmps)`
  The equivalent of Container.enclose() with a box layout Y, with center alignment.
  `boolean`
  `equals(Object o)`
  Indicates whether some other object is "equal to" this one.
  `int`
  `getAlign()`
  Gets the alignment of this layout.
  `int`
  `getAxis()`
  Returns the layout axis x/y
  `Dimension`
  `getPreferredSize(Container parent)`
  Returns the container preferred size
  `int`
  `hashCode()`
  Returns a hash code value for the object.
  `void`
  `layoutContainer(Container parent)`
  Layout the given parent container children
  `void`
  `setAlign(int align)`
  Sets the alignment of this layout.
  `String`
  `toString()`
  Returns a string representation of the object.
  `static BoxLayout`
  `x()`
  Shorthand for `new BoxLayout(BoxLayout.X_AXIS)`
  `static BoxLayout`
  `xCenter()`
  Creates a new layout with `#X_AXIS`, and align center.
  `static BoxLayout`
  `xRight()`
  Creates a new layout with `#X_AXIS`, and align right.
  `static BoxLayout`
  `y()`
  Shorthand for `new BoxLayout(BoxLayout.Y_AXIS)`
  `static BoxLayout`
  `yBottom()`
  Creates a new layout with `#Y_AXIS`, and align bottom.
  `static BoxLayout`
  `yCenter()`
  Creates a new layout with `#Y_AXIS`, and align center.
  `static BoxLayout`
  `yLast()`
  Shorthand for `new BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)`

  ### 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`

- ## Field Details

  - ### X_AXIS

    public static final int X_AXIS
    Horizontal layout where components are arranged from left to right
    See Also:
      - Constant Field Values

  - ### Y_AXIS

    public static final int Y_AXIS
    Vertical layout where components are arranged from top to bottom
    See Also:
      - Constant Field Values

  - ### X_AXIS_NO_GROW

    public static final int X_AXIS_NO_GROW
    Horizontal layout where components are arranged from left to right but don't grow vertically beyond their preferred size
    See Also:
      - Constant Field Values

  - ### Y_AXIS_BOTTOM_LAST

    public static final int Y_AXIS_BOTTOM_LAST
    Same as Y_AXIS with a special case for the last component. The last component is glued to the end of the available space
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### BoxLayout

    public BoxLayout(int axis)

    Creates a new instance of BoxLayout

    ###### Parameters

    - `axis`: @param axis the axis to lay out components along. Can be: BoxLayout.X_AXIS or BoxLayout.Y_AXIS

- ## Method Details

  - ### y

    public static BoxLayout y()

    Shorthand for `new BoxLayout(BoxLayout.Y_AXIS)`

    ###### Returns

    a new Y axis `BoxLayout`

  - ### yLast

    public static BoxLayout yLast()

    Shorthand for `new BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)`

    ###### Returns

    a new Y bottom last axis `BoxLayout`

  - ### yCenter

    public static BoxLayout yCenter()

    Creates a new layout with `#Y_AXIS`, and align center.

    ###### Returns

    BoxLayout with center alignment on Y_AXIS.

    ###### Since

    7.0

  - ### yBottom

    public static BoxLayout yBottom()

    Creates a new layout with `#Y_AXIS`, and align bottom.

    ###### Returns

    BoxLayout with bottom alignment on Y_AXIS.

    ###### Since

    7.0

  - ### x

    public static BoxLayout x()

    Shorthand for `new BoxLayout(BoxLayout.X_AXIS)`

    ###### Returns

    a new X axis `BoxLayout`

  - ### xCenter

    public static BoxLayout xCenter()

    Creates a new layout with `#X_AXIS`, and align center.

    ###### Returns

    BoxLayout with center alignment on X_AXIS.

    ###### Since

    7.0

  - ### xRight

    public static BoxLayout xRight()

    Creates a new layout with `#X_AXIS`, and align right.

    ###### Returns

    BoxLayout with right alignment on X_AXIS.

    ###### Since

    7.0

  - ### encloseY

    public static Container encloseY(Component... cmps)

    The equivalent of Container.enclose() with a box layout Y

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

  - ### encloseYCenter

    public static Container encloseYCenter(Component... cmps)

    The equivalent of Container.enclose() with a box layout Y, with center alignment.

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

    ###### Since

    7.0

  - ### encloseYBottom

    public static Container encloseYBottom(Component... cmps)

    The equivalent of Container.enclose() with a box layout Y, with bottom alignment.

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

    ###### Since

    7.0

  - ### encloseYBottomLast

    public static Container encloseYBottomLast(Component... cmps)

    The equivalent of Container.enclose() with a box layout Y in bottom last mode

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

  - ### encloseX

    public static Container encloseX(Component... cmps)

    The equivalent of Container.enclose() with a box layout X

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

  - ### encloseXNoGrow

    public static Container encloseXNoGrow(Component... cmps)

    The equivalent of Container.enclose() with a box layout X no grow option

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

  - ### encloseXCenter

    public static Container encloseXCenter(Component... cmps)

    The equivalent of Container.enclose() with a box layout X, with center alignment.

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

    ###### Since

    7.0

  - ### encloseXRight

    public static Container encloseXRight(Component... cmps)

    The equivalent of Container.enclose() with a box layout X, with right alignment.

    ###### Parameters

    - `cmps`: the set of components

    ###### Returns

    the newly created container

    ###### Since

    7.0

  - ### getAlign

    public int getAlign()

    Gets the alignment of this layout. By default Y_AXIS aligns top, and X_AXIS aligns left (RTL-aware). You can specify an align value of `Component#CENTER` to align items vertically centered (for Y_AXIS), and horizontally centered (for X_AXIS), of `Component#BOTTOM` to align vertically bottom (Y_AXIS), and `Component#RIGHT` to align right (RTL-aware), for X_AXIS.

    ###### Returns

    The alignment.

    ###### Since

    7.0

  - ### setAlign

    public void setAlign(int align)

    Sets the alignment of this layout. By default Y_AXIS aligns top, and X_AXIS aligns left (RTL-aware). You can specify an align value of `Component#CENTER` to align items vertically centered (for Y_AXIS), and horizontally centered (for X_AXIS), of `Component#BOTTOM` to align vertically bottom (Y_AXIS), and `Component#RIGHT` to align right (RTL-aware), for X_AXIS.

    ###### Parameters

    - `align`: One of `Component#CENTER`, `Component#BOTTOM`, `Component#RIGHT`, to adjust the alignment of children.

    ###### Since

    7.0

  - ### layoutContainer

    public void layoutContainer(Container parent)

    Layout the given parent container children

    ###### Parameters

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

  - ### 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`

  - ### getAxis

    public int getAxis()

    Returns the layout axis x/y

    ###### Returns

    the layout axis

  - ### 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`

  - ### 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`