Flex layout
Lists, menus and toolbars put their items one after the other.
A FlexNode does it for you: it stacks its children in a column or a row on every frame, and grows to hold them.
You will learn
- How to stack children in a column or a row, with a gap between them
- How to align children across the line, or shift one of them
- How a flex node takes its size
- How to nest rows inside a column
Stack children in a column
Section titled “Stack children in a column”FlexNode.vertical creates a column from its position and its width:
FlexNode.vertical(20, 20, 360).margin(10D).body(flex -> { RectNode.create(0, 0, 200, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 120, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 280, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 160, 50).color(FlexCasesUI.ACCENT).attach(flex);}).attach(card);
FlexNode.margin is the gap between two children, in design pixels; there is none before the first child nor after the last.
The column is a layout node: it places its children again on every frame, so a child that grows pushes the next ones down.
A hidden, removed or ABSOLUTE child gets no place in the line (Know who places a child), and the next children close the gap from the next frame.
Line children up in a row
Section titled “Line children up in a row”FlexNode.horizontal creates a row from its position and its height:
FlexNode.horizontal(20, 20, 260).margin(10D).body(flex -> { RectNode.create(0, 0, 60, 120).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 90, 200).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 60, 260).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 120, 80).color(FlexCasesUI.ACCENT).attach(flex);}).attach(card);
FlexNode.direction turns a column into a row or back, with an Axis: .direction(Axis.HORIZONTAL).
Align children across the line
Section titled “Align children across the line”FlexNode.align takes an Align and places every child across the line: on the width of a column, on the height of a row.
FlexNode.vertical(20, 20, 360).margin(10D).align(Align.CENTER).body(flex -> { RectNode.create(0, 0, 200, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 120, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 280, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 160, 50).color(FlexCasesUI.ACCENT).attach(flex);}).attach(card);
Align.CENTER centers each bar on the 360 design pixels of the column.
Align.START puts the children against the left of a column or the top of a row, Align.END against the right or the bottom.
Without align, the default, each child keeps its own position across the line: its x in a column, its y in a row.
Shift one child
Section titled “Shift one child”The own position of a child adds to the place that the flex node gives it:
FlexNode.vertical(20, 20, 360).margin(10D).body(flex -> { RectNode.create(0, 0, 200, 50).color(FlexCasesUI.ELEMENT).attach(flex); RectNode.create(40, 30, 200, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 200, 50).color(FlexCasesUI.ELEMENT).attach(flex);}).attach(card);
Along the line, the offset moves the child alone: the next children keep their place, so an offset larger than the margin makes the child overlap the next one.
Across the line, the offset applies only while align is not set: an alignment places the child exactly.
Let the flex node take its size
Section titled “Let the flex node take its size”A flex node created with a length of 0 along its line takes the length of its content: a column as high as its children and their gaps, a row as wide. Here a dark grey panel reads the height of the column to wrap it:
final FlexNode list = FlexNode.vertical(20, 20, 320).margin(10D).body(flex -> { RectNode.create(0, 0, 320, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 320, 50).color(FlexCasesUI.ACCENT).attach(flex); RectNode.create(0, 0, 320, 50).color(FlexCasesUI.ACCENT).attach(flex);});RectNode.create(20, 20, 360, 0).color(FlexCasesUI.ELEMENT).height(() -> list.getHeight() + 40D).body(panel -> list.attach(panel)).attach(card);
The panel keeps wrapping the column when bars appear or disappear:
A length that you give with Node.height on a column, or Node.width on a row, follows the rule of the layout nodes: with an OverflowProperty other than NONE, it stays fixed and the children scroll inside it.
Nest rows inside a column
Section titled “Nest rows inside a column”A flex node is a node like any other: a column of rows builds a list whose lines hold several items.
FlexNode.vertical(40, 40, 520).margin(20D).body(column -> { for (final String name : names) { FlexNode .horizontal(0, 0, 90) .margin(24D) .align(Align.CENTER) .body(row -> { CircleNode.create(0, 0, 50).color(SettingsListUI.ACCENT).attach(row); TextNode.create(0, 0).text(Text.create(name, info)).attach(row); }) .attach(column); }}).attach(card);Full file: SettingsListUI.java

Each row takes the width of its content, and the column gives the rows their place.
A row that the user can drag into another order is a ReorderableFlexNode (Reorderable lists).
FlexNode.vertical(x, y, width)stacks children in a column,FlexNode.horizontal(x, y, height)in a row, in attach order.marginis the gap between two children.alignplaces every child across the line; without it, each child keeps its own cross position.- The own offset of a child adds to its place; along the line, it moves that child only.
- A length of 0 follows the content; a declared length follows the rule of the layout nodes.
Try it
Section titled “Try it”A toolbar. Build a row of five blue squares of 60 design pixels, 15 design pixels apart, centered on a row 100 design pixels high.
Solution
The row is 100 high; align(Align.CENTER) centers each square on that height:
FlexNode.horizontal(20, 20, 100).margin(15D).align(Align.CENTER).body(flex -> { for (int i = 0; i < 5; i++) { RectNode.create(0, 0, 60, 60).color(ToolbarUI.ACCENT).attach(flex); }}).attach(this);Indent one line.
In SettingsListUI, indent the line Music by 40 design pixels without changing the other lines.
Solution
The column has no align, so the own x of a row stays: create the Music row at x = 40, for example with FlexNode.horizontal(name.equals("Music") ? 40 : 0, 0, 90).
