Skip to content

Nodes and the tree

Everything a UI shows is a node: a rectangle, a text, an image, a list, a slider. Nodes hold other nodes, and together they form the node tree of the UI. This page shows how to create nodes, nest them and change the tree.

You will learn

  • How to create a node, configure it and attach it
  • How to nest nodes with body
  • How positions follow the tree
  • How to build, move, raise and remove parts of the tree
  • How to chain setters without a compile error, and reach the node inside a chain

Every node type has a create(...) factory, setters that return the node, and attach(...):

RectNode.create(660, 490, 600, 100).color(ToolbarUI.SURFACE).attach(this);

Full file: ToolbarUI.java

RectNode.create creates a rectangle from x, y, width and height, color(...) sets one of its properties and returns the node, and attach(this) adds it to the UI. You configure a node in one chain, one setter per property. Once attached, the node is drawn on every frame until you remove it.

Most setters also take a Supplier, so that the property follows a value that changes: Signals answers the question “When does JOID compute this value again?” for each form.

Node.body(Consumer) runs a lambda right away with the node, so you create its children where you create the node. attach(card) attaches a child to its parent:

RectNode
.create(660, 290, 600, 500)
.color(ProfileUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(ProfileUI.ELEMENT, 1.5F))
.body(card -> {
RectNode.create(40, 40, 520, 80).color(ProfileUI.ACCENT).attach(card);
RectNode
.create(40, 150, 520, 310)
.color(ProfileUI.ELEMENT)
.body(content -> {
CircleNode.create(30, 30, 120).color(ProfileUI.MUTED).attach(content);
RectNode.create(180, 50, 300, 30).color(ProfileUI.MUTED).attach(content);
RectNode.create(180, 100, 200, 30).color(ProfileUI.MUTED).attach(content);
})
.attach(card);
})
.attach(this);

Full file: ProfileUI.java

A dark card with a blue header bar, and below it a dark grey panel holding a grey disc and two grey lines of different lengths

The code reads like the result: each body holds what is drawn inside its node.

The lambda parameter is the node itself: use it to attach children and to read its size. Name it after its role (card, content), so that each attach(...) says where the child goes.

A child is placed relative to its parent. Here the first bar of the menu card of UIs sits at (50, 130) in the card, and the card at (760, 340) on the canvas:

On the left, a tree: MenuUI holds a RectNode named card at (760, 340), which holds a TextNode and a highlighted RectNode named bar at (50, 130). On the right, a miniature canvas shows the card with the word Text at its top and its bar highlighted under it. The labels read: card at (760, 340) on the canvas; bar at (50, 130) in the card; so bar at (810, 470) on the canvas.

Positions add up along the tree: move the card and everything in it follows.
  • The nodes attached to the UI are the top of the tree; their position is on the canvas.
  • Every other node is positioned from the top-left corner of its parent.
  • A child is drawn above its parent, and the children are drawn in the order you attach them, the last on top.

Node.getAbsoluteX and Node.getAbsoluteY give the position of a node on the canvas. Position and size covers sizing, anchors and the drawing order.

When a method builds a branch, or attaches it later, build the branch first and attach its root last. Node.append attaches several children at once:

final RectNode toolbar = RectNode.create(660, 290, 600, 100).color(ToolbarUI.SURFACE);
final RectNode back = RectNode.create(20, 20, 60, 60).color(ToolbarUI.ELEMENT);
final RectNode close = RectNode.create(520, 20, 60, 60).color(ToolbarUI.ELEMENT);
toolbar.append(back, close).attach(this);

Full file: ToolbarUI.java

The children of a branch that is not attached yet wait: they join the UI with their root.

A node has one parent. Attaching it to another node, or to the UI, moves it there with its children.

Node.remove detaches children from a node, and Node.clearChildren detaches all of them; UI.remove detaches nodes from the top of the tree:

toolbar.remove(close);

Full file: ToolbarUI.java

A detached node stops drawing, reacting and following its signals. Attached again, it starts again: Node lifecycle details what runs at each step.

Each click attaches a new child to the row or removes its last child; the row draws exactly the children it holds.

To rebuild a whole branch when data changes, let JOID run the body again for you: Rebuilding the tree shows how.

Attaching a node that is already attached to the same parent moves it to the end of its siblings, so it is drawn on top of them:

final RectNode first = RectNode.create(660, 690, 300, 100).color(ToolbarUI.ACCENT).attach(this);
RectNode.create(760, 720, 300, 100).color(ToolbarUI.ELEMENT).attach(this);
first.attach(this);

Full file: ToolbarUI.java

The blue rectangle is attached first, then the dark grey one covers it; attaching the blue one again puts it on top.

Each click attaches the clicked rectangle to the card again, so it becomes the last child and is drawn on top.

To keep a node above or below its siblings whatever the attach order, give it a zindex (Position and size).

Node.getParent returns the parent, or null at the top of the tree, and Node.getChildren() returns every child, in attach order. Node.getChildren(Class) keeps the children of one class, and Node.getChild returns one of them by index, or null:

for (final RectNode button : toolbar.getChildren(RectNode.class)) {
button.hoveredColor(ToolbarUI.ACCENT);
}
toolbar.getChild(0, RectNode.class).color(ToolbarUI.ACCENT);

Full file: ToolbarUI.java

The nodes at the top of the tree are in UI.getNodeList, in drawing order, and Node.getUi returns the UI of a node. These lists are read-only views: change the tree with attach, append and remove.

Setters return the node, typed by what the compiler expects next. The setters of a node type, such as color of RectNode, return that type. The setters that every node has, such as visible, onClick or attach, return a plain Node in the middle of a chain.

So call the setters of the type first, then the shared ones:

RectNode.create(0, 0, 100, 100).visible(true).color(ProfileUI.ACCENT); // ✗ visible returns a Node: no color
RectNode.create(0, 0, 100, 100).color(ProfileUI.ACCENT).visible(true); // ✓ the RectNode setters come first

When the order cannot change, give the type to the shared setter: .<RectNode>visible(true).color(...). At the end of a chain, the variable you assign gives the type: final RectNode first = RectNode.create(...).color(...).attach(this); compiles.

In the middle of a chain, the node has no variable yet. Node.self runs a lambda right away with the node, so that a setter can read the node itself:

RectNode
.create(1300, 290, 200, 0)
.color(ToolbarUI.ELEMENT)
.self(bar -> bar.height(() -> bar.getWidth() / 4D))
.attach(this);

Full file: ToolbarUI.java

Here the height of the bar follows its width. Unlike body, which JOID runs again when it rebuilds the children, self runs once.

The same type argument types the node that body, self and the listeners receive: in the middle of a chain, their lambda receives a plain Node. .<RectNode>onClick(event -> event.getNode().color(...)) gives the listener a RectNode, so that it can call the setters of the type.

The Catalog shows every node with a capture. These pages teach them by role:

  • Every node comes from a create(...) factory and is configured by fluent setters, one per property.
  • attach(this) adds a node to the UI; attach(parent) adds it to another node.
  • body(...) runs right away and creates the children inside their parent.
  • A child is positioned from its parent and drawn above it; siblings are drawn in attach order.
  • A node has one parent: attaching it elsewhere moves it; remove(...) detaches it.
  • Attaching a node again brings it to the front of its siblings.
  • Call the type setters before the shared setters in a chain; self(...) hands you the node in the middle of a chain.

Add a badge. Add a small dark disc in the top-right corner of the header of ProfileUI.

Solution

Give the header a body: the disc is positioned from the header, not from the card.

RectNode
.create(40, 40, 520, 80)
.color(ProfileUI.ACCENT)
.body(header -> CircleNode.create(470, 15, 50).color(ProfileUI.ELEMENT).attach(header))
.attach(card);

Fix the chain. This line does not compile. Make it compile without changing what it does.

CircleNode.create(0, 0, 40).onClick(event -> JOID.close(this)).color(ProfileUI.ACCENT).attach(this);
Solution

onClick is shared by every node: call color first.

CircleNode.create(0, 0, 40).color(ProfileUI.ACCENT).onClick(event -> JOID.close(this)).attach(this);

Raise on click. Make each dark grey button of the toolbar come to the front of its siblings when it is clicked.

Solution

Attaching a node again to its parent moves it to the end of its siblings; the event gives the clicked node.

.onClick(event -> event.getNode().attach(toolbar))

Report a problem with this page