Skip to content

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

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:

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

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);
}

Full file: GroupUI.java

On a dark card, two identical groups, the second one lower and further right: each is a dark grey disc followed by a blue bar and a shorter dark grey bar

Each group is one container: its position moves the disc and the two bars at once.

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.

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

Full file: OverflowUI.java

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

Full file: OverflowUI.java

Two dark grey panels side by side on a dark card, each holding the same blue bar that is wider than the panel: on the left the bar stops at the edge of the panel, on the right it continues over the card

The same child, clipped by 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.

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

Full file: FeedUI.java

The panel keeps its size: the wheel moves its eight rows inside it.

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.

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

Full file: FeedUI.java

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.

Four listeners follow the scroll of a node, for both axes; the event names the axis with getAxis():

  • Node.beforeScroll fires before the offset changes; cancel() keeps the node where it is, and its scrollbar thumb with it, even under a drag.
  • Node.onScroll fires once the offset changed; getPrevious() and getOffset() give both offsets.
  • Node.onScrollToEnd fires when a scroll heads to the end of the content.
  • Node.onScrollEndReached fires 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);
}
});

Full file: FeedUI.java

The node measures its new content on the next frame, and the wheel scrolls further.

The list grows each time its end is reached, and scrollTo(0D, 0D) glides back to the start as the wheel does.

Events explains listeners, their phases and cancellation.

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

Full file: ScrollbarUI.java

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.

The thumb follows the wheel, and dragging the thumb scrolls the list.

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 own x and y stay an offset.
  • A ContainerNode groups nodes and draws nothing.
  • overflow decides what goes past a node: NONE draws it, HIDDEN clips it, SCROLL clips it and scrolls.
  • The wheel scrolls 30 design pixels per notch times scrollSpeed; the content glides to its offset.
  • Offsets go from 0D down to minus the overflow; scrollTo scrolls like the user, updateScroll jumps.
  • onScrollEndReached fires when the content arrives at its end: append more there.

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

Report a problem with this page