Skip to content

TreeList

Since 2.2.1

TreeList<NODE> is a generic hierarchical list widget. Each node implements ITreeNode and can be a branch (expandable) or a leaf. Nodes are rendered using a configurable UIElementProvider and can be expanded/collapsed via arrow icons, single-click, or double-click.

Features:

  • Single and multi-selection (with Shift and Ctrl).
  • Dynamic tree (children are rechecked every tick by default) or static tree.
  • Flatten-root mode to skip the root node in the display.
  • Customisable expand/collapse icons and node background textures.

INFO

Everything documented on UIElement (layout, styles, events, data bindings, etc.) applies here too.


Usage

java
var tree = new TreeList<MyNode>(rootNode);
tree.setNodeUISupplier(TreeList.textTemplate(node -> Component.literal(node.getName())));
tree.setOnSelectedChanged(selected -> System.out.println("Selected: " + selected));
tree.setDoubleClickToExpand(true);
parent.addChild(tree);

Tree List Style

INFO

node-background

Background texture for unselected nodes.

Default: none (empty)

java
tree.menuStyle(style -> style.nodeTexture(myBg));

INFO

node-hover-background

Background texture for selected (highlighted) nodes.

Default: ColorPattern.BLUE.rectTexture()

java
tree.menuStyle(style -> style.hoverTexture(myHighlight));

INFO

collapse-icon / expand-icon

Icons shown on branch nodes when they are collapsed or expanded.

Defaults: Icons.RIGHT_ARROW_NO_BAR_S_WHITE / Icons.DOWN_ARROW_NO_BAR_S_WHITE

java
tree.menuStyle(style -> style
    .collapseIcon(myCollapsed)
    .expandIcon(myExpanded)
);

Fields

NameTypeAccessDescription
treeListStyleTreeListStyleprivate (getter)Current style.
rootNODE (nullable)private (getter)The root tree node.
hoveredNodeNODE (nullable)private (getter)The node currently under the mouse.
doubleClickToExpandbooleansetterWhether double-clicking expands/collapses branches. Default: true.
clickToExpandbooleansetterWhether single-clicking expands/collapses branches. Default: false.
supportMultipleSelectionbooleansetterWhether Ctrl+click and Shift+click select multiple nodes. Default: false.
staticTreebooleansetterWhen true, children are not rechecked every tick. Default: false.

Methods

MethodReturnsDescription
setRoot(NODE)TreeList&lt;NODE&gt;Sets the root node and rebuilds the list.
setNodeUISupplier(UIElementProvider&lt;NODE&gt;)TreeList&lt;NODE&gt;Sets the factory that creates the UI for each node.
setOnNodeUICreated(BiConsumer&lt;NODE, UIElement&gt;)TreeList&lt;NODE&gt;Callback invoked after each node UI is created.
setOnSelectedChanged(Consumer&lt;Set&lt;NODE&gt;&gt;)Callback invoked when the selection changes.
setOnDoubleClickNode(Consumer&lt;NODE&gt;)Callback invoked on double-click.
setSelectableNodeFilter(Predicate&lt;NODE&gt;)Predicate that controls which nodes can be selected.
setFlattenRoot(boolean)TreeList&lt;NODE&gt;When true, the root is not shown; its children become top-level items.
getSelected()Set&lt;NODE&gt;Returns the currently selected nodes (unmodifiable).
setSelected(Collection&lt;NODE&gt;, boolean)TreeList&lt;NODE&gt;Replaces the selection.
addSelected(NODE, boolean)TreeList&lt;NODE&gt;Adds a node to the selection.
removeSelected(NODE, boolean)TreeList&lt;NODE&gt;Removes a node from the selection.
expandNode(NODE)voidExpands a branch node.
collapseNode(NODE)voidCollapses a branch node.
expandNodeAlongPath(NODE)voidExpands all ancestors of the given node.
isNodeExpanded(NODE)booleanReturns true if the node is currently expanded.
isNodeSelected(NODE)booleanReturns true if the node is currently selected.
reloadList()TreeList&lt;NODE&gt;Clears and rebuilds all node UIs from the current root.
menuStyle(Consumer&lt;TreeListStyle&gt;)TreeList&lt;NODE&gt;Configures style fluently.

Node UI Templates

Static helper methods for common node UI layouts:

MethodDescription
textTemplate(Function&lt;NODE, Component&gt;)Creates a node UI showing only text.
iconTextTemplate(Function&lt;NODE, IGuiTexture&gt;, Function&lt;NODE, Component&gt;)Creates a node UI with an icon and text.
optionalIconTextTemplate(Function&lt;NODE, IGuiTexture&gt;, Function&lt;NODE, Component&gt;)Like iconTextTemplate but the icon is shown only when non-empty.

Released under the MIT License.