public 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.
Fields
public static final int X_AXIS = 1 | Horizontal layout where components are arranged from left to right |
public static final int Y_AXIS = 2 | Vertical layout where components are arranged from top to bottom |
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 |
public static final int Y_AXIS_BOTTOM_LAST = 4 | Same as Y_AXIS with a special case for the last component. |
Constructors
public BoxLayout(int axis) | Creates a new instance of BoxLayout |
Methods
Inherited methods
Field details
X_AXIS
public static final int X_AXIS = 1Y_AXIS
public static final int Y_AXIS = 2X_AXIS_NO_GROW
public static final int X_AXIS_NO_GROW = 3Y_AXIS_BOTTOM_LAST
public static final int Y_AXIS_BOTTOM_LAST = 4Constructor details
BoxLayout
public BoxLayout(int axis)Parameters
axisint- the axis to lay out components along. Can be: BoxLayout.X_AXIS or BoxLayout.Y_AXIS
Method details
y
public static BoxLayout y()new BoxLayout(BoxLayout.Y_AXIS)Returns
BoxLayoutyLast
public static BoxLayout yLast()new BoxLayout(BoxLayout.Y_AXIS_BOTTOM_LAST)Returns
BoxLayoutyCenter
public static BoxLayout yCenter()#Y_AXIS, and align center.Returns
yBottom
public static BoxLayout yBottom()#Y_AXIS, and align bottom.Returns
x
public static BoxLayout x()new BoxLayout(BoxLayout.X_AXIS)Returns
BoxLayoutxCenter
public static BoxLayout xCenter()#X_AXIS, and align center.Returns
xRight
public static BoxLayout xRight()#X_AXIS, and align right.Returns
encloseY
public static Container encloseY(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseYCenter
public static Container encloseYCenter(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseYBottom
public static Container encloseYBottom(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseYBottomLast
public static Container encloseYBottomLast(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseX
public static Container encloseX(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseXNoGrow
public static Container encloseXNoGrow(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseXCenter
public static Container encloseXCenter(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
encloseXRight
public static Container encloseXRight(Component... cmps)Parameters
cmpsComponent...- the set of components
Returns
getAlign
public int getAlign()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
setAlign
public void setAlign(int align)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
alignint- One of
Component#CENTER,Component#BOTTOM,Component#RIGHT, to adjust the alignment of children.
layoutContainer
public void layoutContainer(Container parent)Parameters
parentContainer- the given parent container
getPreferredSize
public Dimension getPreferredSize(Container parent)Parameters
parentContainer- the parent container
Returns
getAxis
public int getAxis()Returns
toString
public String toString()equals
public boolean equals(Object o)hashCode
public int hashCode()