public class BoxLayout

  1. Object
  2. Layout
  3. BoxLayout

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.

Fields

public static final int X_AXIS = 1Horizontal layout where components are arranged from left to right
public static final int Y_AXIS = 2Vertical layout where components are arranged from top to bottom
public static final int X_AXIS_NO_GROW = 3Horizontal layout where components are arranged from left to right but don’t grow vertically beyond their preferred size
public static final int Y_AXIS_BOTTOM_LAST = 4Same as Y_AXIS with a special case for the last component.

Constructors

public BoxLayout(int axis)Creates a new instance of BoxLayout

Methods

public static BoxLayout y()Shorthand for new BoxLayout(BoxLayout.Y_AXIS)
public static BoxLayout yLast()Shorthand for new BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)
public static BoxLayout yCenter()Creates a new layout with #Y_AXIS, and align center.
public static BoxLayout yBottom()Creates a new layout with #Y_AXIS, and align bottom.
public static BoxLayout x()Shorthand for new BoxLayout(BoxLayout.X_AXIS)
public static BoxLayout xCenter()Creates a new layout with #X_AXIS, and align center.
public static BoxLayout xRight()Creates a new layout with #X_AXIS, and align right.
public static Container encloseY(Component... cmps)The equivalent of Container.enclose() with a box layout Y
public static Container encloseYCenter(Component... cmps)The equivalent of Container.enclose() with a box layout Y, with center alignment.
public static Container encloseYBottom(Component... cmps)The equivalent of Container.enclose() with a box layout Y, with bottom alignment.
public static Container encloseYBottomLast(Component... cmps)The equivalent of Container.enclose() with a box layout Y in bottom last mode
public static Container encloseX(Component... cmps)The equivalent of Container.enclose() with a box layout X
public static Container encloseXNoGrow(Component... cmps)The equivalent of Container.enclose() with a box layout X no grow option
public static Container encloseXCenter(Component... cmps)The equivalent of Container.enclose() with a box layout X, with center alignment.
public static Container encloseXRight(Component... cmps)The equivalent of Container.enclose() with a box layout X, with right alignment.
public int getAlign()Gets the alignment of this layout.
public void setAlign(int align)Sets the alignment of this layout.
public void layoutContainer(Container parent)Layout the given parent container children
public Dimension getPreferredSize(Container parent)Returns the container preferred size
public int getAxis()Returns the layout axis x/y
public String toString()Returns a string representation of the object.
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

Field details

X_AXIS

public static final int X_AXIS = 1
Horizontal layout where components are arranged from left to right

Y_AXIS

public static final int Y_AXIS = 2
Vertical layout where components are arranged from top to bottom

X_AXIS_NO_GROW

public static final int X_AXIS_NO_GROW = 3
Horizontal layout where components are arranged from left to right but don’t grow vertically beyond their preferred size

Y_AXIS_BOTTOM_LAST

public static final int Y_AXIS_BOTTOM_LAST = 4
Same as Y_AXIS with a special case for the last component. The last component is glued to the end of the available space

Constructor details

BoxLayout

public BoxLayout(int axis)
Creates a new instance of BoxLayout

Parameters

axis int
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.

yBottom

public static BoxLayout yBottom()
Creates a new layout with #Y_AXIS, and align bottom.

Returns

BoxLayout with bottom alignment on Y_AXIS.

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.

xRight

public static BoxLayout xRight()
Creates a new layout with #X_AXIS, and align right.

Returns

BoxLayout with right alignment on X_AXIS.

encloseY

public static Container encloseY(Component... cmps)
The equivalent of Container.enclose() with a box layout Y

Parameters

cmps Component...
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 Component...
the set of components

Returns

the newly created container

encloseYBottom

public static Container encloseYBottom(Component... cmps)
The equivalent of Container.enclose() with a box layout Y, with bottom alignment.

Parameters

cmps Component...
the set of components

Returns

the newly created container

encloseYBottomLast

public static Container encloseYBottomLast(Component... cmps)
The equivalent of Container.enclose() with a box layout Y in bottom last mode

Parameters

cmps Component...
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 Component...
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 Component...
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 Component...
the set of components

Returns

the newly created container

encloseXRight

public static Container encloseXRight(Component... cmps)
The equivalent of Container.enclose() with a box layout X, with right alignment.

Parameters

cmps Component...
the set of components

Returns

the newly created container

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.

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 int
One of Component#CENTER, Component#BOTTOM, Component#RIGHT, to adjust the alignment of children.

layoutContainer

public void layoutContainer(Container parent)
Layout the given parent container children

Parameters

parent Container
the given parent container

getPreferredSize

public Dimension getPreferredSize(Container parent)
Returns the container preferred size

Parameters

parent Container
the parent container

Returns

the container preferred size

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())

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).

hashCode

public int hashCode()
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.)