Layout
A child sits where you put it, relative to its parent, until a layout node places it. This page gives the rule that says which nodes place their children, then shows how to group nodes, clip them and scroll them.
You will learn
- Which nodes place their children and which leave them alone
- How to group nodes without drawing anything
- How to clip what goes beyond a node
- How to scroll a node with the wheel and from your code
- How to react when a node scrolls or reaches its end
Know who places a child
Section titled “Know who places a child”Every child is positioned from the top-left corner of its parent (Nodes and the tree).
A node never moves nor resizes its children: a RectNode with three children draws them at the x and y you gave them.
A layout node places its children itself, on every frame:
FlexNodelines them up in a row or a column (Flex layout).GridNodefills rows of cells and wraps (Grid layout).ReorderableFlexNodeis a flex layout whose children the user drags into another order (Reorderable lists).
A layout node adds a layout position to the position of the child.
The x and y of the child stay an offset on top of it, so the child moves by them inside its slot.
A layout node gives no slot to a child that is not visible, nor to a child whose position is ABSOLUTE (Position and size).
To place children by a rule of your own, write a node that overrides its layout hook (Custom nodes).
Group nodes without drawing them
Section titled “Group nodes without drawing them”A ContainerNode draws nothing.
It holds children, so that they move, hide and clip together:
for (int i = 0; i < 2; i++) { ContainerNode .create(40 + i * 60, 40 + i * 120, 400, 100) .body(group -> { CircleNode.create(0, 0, 100).color(GroupUI.ELEMENT).attach(group); RectNode.create(130, 15, 270, 30).color(GroupUI.ACCENT).attach(group); RectNode.create(130, 60, 180, 25).color(GroupUI.ELEMENT).attach(group); }) .attach(card);}
Give the container the size of its content when you clip it, scroll it or test it under the mouse: these use its bounds. A container sized 0 still draws its children, since a node does not clip by default.
Clip what goes beyond a node
Section titled “Clip what goes beyond a node”Node.overflow decides what happens to the children that go past the bounds of a node.
OverflowProperty.HIDDEN clips them:
RectNode.create(20, 20, 360, 260).color(OverflowUI.ELEMENT).overflow(OverflowProperty.HIDDEN).body(area -> RectNode.create(40, 60, 360, 140).color(OverflowUI.ACCENT).attach(area)).attach(card);OverflowProperty.NONE, the default, lets them draw over the rest of the UI:
RectNode.create(440, 20, 360, 260).color(OverflowUI.ELEMENT).body(area -> RectNode.create(40, 60, 360, 140).color(OverflowUI.ACCENT).attach(area)).attach(card);
HIDDEN on the left and drawn whole with NONE on the right.
A clipped part of a child is not drawn and does not receive the mouse.
With NONE, the node counts the children that go past it in its own extent, so a scrolling ancestor scrolls far enough to show them.
Scroll a node
Section titled “Scroll a node”OverflowProperty.SCROLL clips the children like HIDDEN and lets the user scroll through them with the wheel:
final RectNode feed = RectNode.create(20, 20, 440, 320).color(FeedUI.ELEMENT).overflow(OverflowProperty.SCROLL).body(area -> { for (int i = 0; i < 8; i++) { RectNode .create(20, 20 + i * 80, 400, 60) .color(FeedUI.ACCENT) .label(Text.create(String.valueOf(i + 1), info)) .attach(area); }}).attach(card);The node measures its content on every frame: the bottom and the right edge of its farthest child, ABSOLUTE children aside.
The gap of the first child from the top-left corner is kept after the last child too, so the content ends with the same margin it starts with.
Node.hasOverflowX and Node.hasOverflowY tell whether the content is larger than the node on each axis.
A flex or a grid created with a length of 0 takes the length of its content, and a length that you give it is a minimum that it grows past when the content needs more room.
With an overflow other than NONE, the length that you give is fixed and the content scrolls; a layout node of length 0 grows with its content and never scrolls.
One notch of the wheel scrolls 30 design pixels, times Node.scrollSpeed (1D by default), and twice that while Ctrl is held.
The vertical wheel scrolls horizontally a node whose content overflows only in width.
The content glides to its new offset over a few frames instead of jumping.
The wheel goes to the innermost scrolling node under the mouse that can still scroll; once that node is at its end, the node around it scrolls.
Scroll from your code
Section titled “Scroll from your code”The scroll offset of an axis goes from 0D, the start, down to minus the overflow: at -200D, the content shows what lies 200 design pixels below its start.
RectNode.create(20, 360, 200, 60).color(FeedUI.ACCENT).label(Text.create("Top", info)).onClick(event -> feed.scrollTo(0D, 0D)).attach(card);Node.scrollToscrolls both axes as the wheel does, so the scroll events below fire and abeforeScrolllistener can refuse it.Node.scrollXandNode.scrollYset the offset of one axis, like any setter. Given a writable signal, they follow it and write back the offset that the user scrolls to (Signals).Node.updateScrolljumps to the requested offset at once, without the glide;Node.updateScrollXandNode.updateScrollYdo it for one axis.
JOID keeps every offset between 0D and the end of the content.
A positive offset, such as scrollY(40D), comes back to 0D, with a warning in dev mode: write scrollY(-40D).
Node.getTargetScrollY returns the offset that the node glides to, Node.getScrollY the offset drawn this frame, and Node.getMaxScrollY the overflow, a positive number; the same getters exist for X.
React to scrolling
Section titled “React to scrolling”Four listeners follow the scroll of a node, for both axes; the event names the axis with getAxis():
Node.beforeScrollfires before the offset changes;cancel()keeps the node where it is, and its scrollbar thumb with it, even under a drag.Node.onScrollfires once the offset changed;getPrevious()andgetOffset()give both offsets.Node.onScrollToEndfires when a scroll heads to the end of the content.Node.onScrollEndReachedfires when the content arrives at its end, after the glide.
Append more children when the end is reached, and the list grows as the user reads it:
feed.onScrollEndReached(event -> { final Node area = event.getNode(); final int count = area.getChildren().size(); for (int i = count; i < count + 4; i++) { RectNode .create(20, 20 + i * 80, 400, 60) .color(FeedUI.ACCENT) .label(Text.create(String.valueOf(i + 1), info)) .attach(area); }});The node measures its new content on the next frame, and the wheel scrolls further.
scrollTo(0D, 0D) glides back to the start as the wheel does.
Events explains listeners, their phases and cancellation.
Add a scrollbar
Section titled “Add a scrollbar”Node.scrollbar gives a scrolling node a scrollbar, a control that shows the offset and that the user drags:
.overflow(OverflowProperty.SCROLL).scrollbar( new ScrollbarNode(424, 8, 8, 304) {} .track(RectNode.create(0, 0, 0, 0).color(ScrollbarUI.SURFACE)) .thumb(RectNode.create(0, 0, 0, 0).color(ScrollbarUI.ACCENT)))ScrollbarNode is an abstract control: its empty subclass gets its two parts, the track and the thumb, where it is created, at size 0, since the scrollbar sizes them (Controls explains this form).
Its shape sets its axis: a scrollbar taller than wide scrolls vertically, one wider than tall horizontally.
The scrollbar is positioned from the scrolling node, does not scroll with its content, and is drawn only while the content overflows on its axis.
Its reference page lists its options, and Give your controls a look turns it into a reusable skin.
- A node never moves its children; a layout node (
FlexNode,GridNode,ReorderableFlexNode) places them on every frame, and their ownxandystay an offset. - A
ContainerNodegroups nodes and draws nothing. overflowdecides what goes past a node:NONEdraws it,HIDDENclips it,SCROLLclips it and scrolls.- The wheel scrolls 30 design pixels per notch times
scrollSpeed; the content glides to its offset. - Offsets go from
0Ddown to minus the overflow;scrollToscrolls like the user,updateScrolljumps. onScrollEndReachedfires when the content arrives at its end: append more there.
Try it
Section titled “Try it”Faster wheel.
Make the feed of FeedUI scroll 90 design pixels per notch.
Solution
Thirty design pixels times 3:
.overflow(OverflowProperty.SCROLL).scrollSpeed(3D)Bottom button. Add a second button that jumps to the end of the feed, without the glide.
Solution
The offset is clamped to the end of the content, then updateScroll() skips the glide:
.onClick(event -> feed.scrollTo(0D, -feed.getMaxScrollY()).updateScroll())