Class 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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intHorizontal layout where components are arranged from left to rightstatic final intHorizontal layout where components are arranged from left to right but don't grow vertically beyond their preferred sizestatic final intVertical layout where components are arranged from top to bottomstatic final intSame as Y_AXIS with a special case for the last component. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ContainerThe equivalent of Container.enclose() with a box layout Xstatic ContainerencloseXCenter(Component... cmps) The equivalent of Container.enclose() with a box layout X, with center alignment.static ContainerencloseXNoGrow(Component... cmps) The equivalent of Container.enclose() with a box layout X no grow optionstatic ContainerencloseXRight(Component... cmps) The equivalent of Container.enclose() with a box layout X, with right alignment.static ContainerThe equivalent of Container.enclose() with a box layout Ystatic ContainerencloseYBottom(Component... cmps) The equivalent of Container.enclose() with a box layout Y, with bottom alignment.static ContainerencloseYBottomLast(Component... cmps) The equivalent of Container.enclose() with a box layout Y in bottom last modestatic ContainerencloseYCenter(Component... cmps) The equivalent of Container.enclose() with a box layout Y, with center alignment.booleanintgetAlign()Gets the alignment of this layout.intgetAxis()Returns the layout axis x/ygetPreferredSize(Container parent) Returns the container preferred sizeinthashCode()voidlayoutContainer(Container parent) Layout the given parent container childrenvoidsetAlign(int align) Sets the alignment of this layout.toString()static BoxLayoutx()Shorthand fornew BoxLayout(BoxLayout.X_AXIS)static BoxLayoutxCenter()Creates a new layout with#X_AXIS, and align center.static BoxLayoutxRight()Creates a new layout with#X_AXIS, and align right.static BoxLayouty()Shorthand fornew BoxLayout(BoxLayout.Y_AXIS)static BoxLayoutyBottom()Creates a new layout with#Y_AXIS, and align bottom.static BoxLayoutyCenter()Creates a new layout with#Y_AXIS, and align center.static BoxLayoutyLast()Shorthand fornew BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)
-
Field Details
-
X_AXIS
public static final int X_AXISHorizontal layout where components are arranged from left to right- See Also:
-
Y_AXIS
public static final int Y_AXISVertical layout where components are arranged from top to bottom- See Also:
-
X_AXIS_NO_GROW
public static final int X_AXIS_NO_GROWHorizontal 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_LASTSame 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
Shorthand for
new BoxLayout(BoxLayout.Y_AXIS)Returns
a new Y axis
BoxLayout -
yLast
Shorthand for
new BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)Returns
a new Y bottom last axis
BoxLayout -
yCenter
Creates a new layout with
#Y_AXIS, and align center.Returns
BoxLayout with center alignment on Y_AXIS.
Since
7.0
-
yBottom
Creates a new layout with
#Y_AXIS, and align bottom.Returns
BoxLayout with bottom alignment on Y_AXIS.
Since
7.0
-
x
Shorthand for
new BoxLayout(BoxLayout.X_AXIS)Returns
a new X axis
BoxLayout -
xCenter
Creates a new layout with
#X_AXIS, and align center.Returns
BoxLayout with center alignment on X_AXIS.
Since
7.0
-
xRight
Creates a new layout with
#X_AXIS, and align right.Returns
BoxLayout with right alignment on X_AXIS.
Since
7.0
-
encloseY
-
encloseYCenter
-
encloseYBottom
-
encloseYBottomLast
-
encloseX
-
encloseXNoGrow
-
encloseXCenter
-
encloseXRight
-
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#CENTERto align items vertically centered (for Y_AXIS), and horizontally centered (for X_AXIS), ofComponent#BOTTOMto align vertically bottom (Y_AXIS), andComponent#RIGHTto 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#CENTERto align items vertically centered (for Y_AXIS), and horizontally centered (for X_AXIS), ofComponent#BOTTOMto align vertically bottom (Y_AXIS), andComponent#RIGHTto align right (RTL-aware), for X_AXIS.Parameters
align: One ofComponent#CENTER,Component#BOTTOM,Component#RIGHT, to adjust the alignment of children.
Since
7.0
-
layoutContainer
Layout the given parent container children
Parameters
parent: the given parent container
- Specified by:
layoutContainerin classLayout
-
getPreferredSize
Returns the container preferred size
Parameters
parent: the parent container
Returns
the container preferred size
- Specified by:
getPreferredSizein classLayout
-
getAxis
public int getAxis()Returns the layout axis x/y
Returns
the layout axis
-
toString
-
equals
-
hashCode
-