Skip to content

Give your controls a look

The checkbox, the toggle, the switch, the selector, the slider and the scrollbar of JOID hold a value and react to the mouse, but they draw nothing of their own: each one is an abstract Node. A small subclass, a skin, gives it a look; a set of skins that share their colors forms your UI kit.

You will learn

  • How a control splits its behavior and its look
  • How to skin a control with parts
  • Which parts each control takes
  • How to draw a control entirely your own way
  • How to gather your skins in a UI kit

Each control comes in two classes:

  • Base<X>Node, such as BaseCheckboxNode, holds the behavior: the value, the mouse gestures, beforeChange and onChange, and what a look needs to know, such as getProgress() of a slider.
  • <X>Node, such as CheckboxNode, extends it with named parts: nodes that you give it, and that it places, sizes and animates.

Extend <X>Node for a look made of the usual parts; extend Base<X>Node only when the look does not fit them. Either way, the skin is a subclass with a protected constructor and a create(...) factory, like any custom node, and the value, its signals and its events work as Controls teaches.

Four skins of this page, each redrawn as its value changes. The first three give parts to their control; the pill toggle draws itself from its base class.

A skin passes its parts in its constructor:

public class KitCheckboxNode extends CheckboxNode {
protected KitCheckboxNode(final double x, final double y, final double width, final double height) {
super(x, y, width, height);
super.box(RectNode.create(0, 0, 0, 0).color(KitColors.ELEMENT));
super.mark(RectNode.create(0, 0, width / 2D, height / 2D).color(KitColors.ACCENT));
}
public static KitCheckboxNode create(final double x, final double y, final double size) {
return new KitCheckboxNode(x, y, size, size);
}
}

Full file: KitCheckboxNode.java

KitCheckboxNode.create(100, 100, 40).checked(true).attach(this);
KitIntegerSliderNode.create(100, 180, 300, 40).range(0, 10).value(4).attach(this);
KitSwitchNode.<String>create(100, 260, 300, 50).values("Low", "Medium", "High").value("Medium").attach(this);
KitPillToggleNode.create(100, 350, 80, 40).toggle(true).attach(this);

Full file: KitUI.java

The control places its parts on every frame, so a part is created at 0, 0:

  • A part of size 0 takes the size the control gives it: here the box covers the whole checkbox.
  • A part with a size keeps it, and the control centers or moves it: here the mark stays half the size of the box.
  • The control gives the parts that sit behind the others (box, track, field, background) a zindex of -1 itself, so the other parts and the children you add draw over them.

A part follows the state of its control through a reactive setter, as the thumb of the slider does while it slides:

public class KitIntegerSliderNode extends IntegerSliderNode {
protected KitIntegerSliderNode(final double x, final double y, final double width, final double height) {
super(x, y, width, height);
super.track(RectNode.create(0, 0, 0, 0).color(KitColors.ELEMENT));
super.thumb(RectNode.create(0, 0, 0, 0).color(() -> super.isSliding() ? KitColors.ACTIVE : KitColors.ACCENT));
}
public static KitIntegerSliderNode create(final double x, final double y, final double width, final double height) {
return new KitIntegerSliderNode(x, y, width, height);
}
}

Full file: KitIntegerSliderNode.java

A control without one of its required parts throws when it is attached, for example The KitCheckboxNode has no mark, give it one with mark(...). A control of your own that takes parts sets each one with Node.part, which puts the new part at the place of the previous one among the children.

ControlPartsWhat the control does with them
CheckboxNodebox, mark (required)The box covers the control; the mark is centered and shown while checked.
ToggleNodetrack, knob (required)The track covers the control; the knob is half its width by default and slides from left to right.
SliderNodetrack, thumb (required)The track covers the control; the thumb is a square of its height by default and moves with the value.
SwitchNodeoption((value, index) -> node) (required)Creates one node per value and splits the width between them; a click on one selects its value.
SelectorNodefield (required), option(value -> node) (required)The field covers the list, open or closed; each option is one row of the height of the control, shown while the list is open, and the selected one while it is closed.
ProgressNodefill (required), trackThe track covers the control; the fill covers the part that the progress reaches, from the side of its direction.
ScrollbarNodetrack, thumb (required)The track covers the bar; the thumb is sized and moved from the scroll of the node it serves.
Text fields (FieldNode)backgroundThe background covers the field. A text field draws its text itself and needs no skin.

The switch and the selector call your option function again each time their values change. Text fields shows the hooks of a field of your own, and Charts the skins of a chart.

When the parts do not fit, extend the base class and build the look yourself. This pill toggle slides a circle in a rounded track whose color follows the circle:

public class KitPillToggleNode extends BaseToggleNode<Boolean, Boolean> {
private final TweenAnimator knob;
protected KitPillToggleNode(final double x, final double y, final double width, final double height) {
super(x, y, width, height);
this.knob = TweenAnimator.create(0F);
super.<KitPillToggleNode>onChange(event -> {
this.knob.sequence(250F, event.getValue() ? 1F : 0F, TweenEquations.QUAD_OUT).start();
});
}
public static KitPillToggleNode create(final double x, final double y, final double width, final double height) {
return new KitPillToggleNode(x, y, width, height);
}
@Override
public void init(final UI ui) {
super.clearChildren();
this.knob.value(super.isToggle() ? 1F : 0F);
RectNode
.create(0, 0, super.getWidth(), super.getHeight())
.color(() -> KitColors.ELEMENT.to(KitColors.ACCENT, this.knob.get()))
.effect(RoundedNodeEffect.create((float) super.dh(2D)))
.attach(this);
CircleNode
.create(0, 4, super.getHeight() - 8D)
.color(() -> this.knob.get() > 0.5F ? KitColors.ON_ACCENT : KitColors.TEXT)
.<CircleNode>x(() -> 4D + (super.getWidth() - super.getHeight()) * this.knob.get())
.attach(this);
}
}

Full file: KitPillToggleNode.java

  • Create the children each time the control joins a UI, in init(UI), and call clearChildren() first so that a second opening does not add them twice.
  • Build the look from nodes and effects rather than drawing by hand: they get hover, effects and layout like any node.
  • Animate on a change with a TweenAnimator started in onChange; sequence starts from the value the animator holds, so a quick second click turns the animation back from where it is (Animation teaches animators).
  • Set the animator to the current value in init, so that the control opens in place, without animating.

A listener added in the constructor of a skin lasts as long as the node, as one added in the constructor of a UI does (Open and close a UI).

A UI kit is your set of skins and the colors and sizes they share. Keep these values in one class, and read them from every skin:

public final class KitColors {
public static final Color TEXT = new Color(231, 233, 238).lock();
public static final Color ELEMENT = new Color(38, 43, 53).lock();
public static final Color ACCENT = new Color(91, 140, 255).lock();
public static final Color ACTIVE = new Color(125, 164, 255).lock();
public static final Color ON_ACCENT = new Color(14, 16, 20).lock();
private KitColors() {}
}

Full file: KitColors.java

Changing a color there changes it in every control of every UI. The skins themselves stay small: the controls keep the behavior, so a kit never repeats it. Build a UI kit builds a complete kit, step by step.

  • A control is a Base<X>Node that holds the behavior, extended by an <X>Node that places its parts.
  • A skin passes its parts in its constructor and has a create(...) factory.
  • A part of size 0 takes the size of the control; a part with a size keeps it.
  • The control puts the parts behind the others at zindex(-1) itself; a missing required part throws on attach.
  • A look of your own extends Base<X>Node, builds its children in init(UI) after clearChildren(), and animates in onChange.
  • A UI kit is a set of skins that read their colors and sizes from one place.

A round mark. Draw the mark of the checkbox as a disc.

Solution
super.mark(CircleNode.create(0, 0, width / 2D).color(KitColors.ACCENT));

A slower toggle. Write a toggle skin whose knob takes longer to slide.

Solution

In the constructor of a subclass of ToggleNode, after its track and knob:

super.speed(0.1F);

A lower speed covers a smaller part of the distance on each frame.

Report a problem with this page