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 Details

    • X_AXIS

      public static final int X_AXIS
      Horizontal layout where components are arranged from left to right
      See Also:
    • Y_AXIS

      public static final int Y_AXIS
      Vertical layout where components are arranged from top to bottom
      See Also:
    • 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:
    • 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:
  • 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()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Layout
    • hashCode

      public int hashCode()
      Description copied from class: Layout
      Overrides:
      hashCode in class Layout