Skip to content

Selector

Since 2.2.1

Selector<T> is a generic dropdown picker. Clicking it opens a floating list of candidate items; clicking an item selects it and optionally closes the dropdown. Each candidate is rendered by a configurable UIElementProvider<T>. When the candidate count exceeds max-item, the list switches to a scrollable ScrollerView.

INFO

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


Usage

java
var selector = new Selector<String>();
selector.setCandidates(List.of("Alpha", "Beta", "Gamma"));
selector.setSelected("Alpha");
selector.setOnValueChanged(value -> {
    System.out.println("Selected: " + value);
});
parent.addChild(selector);

Custom Candidate UI

By default each candidate is displayed as its toString() text. Supply a custom provider to render items however you like:

java
selector.setCandidateUIProvider(candidate ->
    new UIElement()
        .addChild(new Label().setText(candidate.displayName(), false))
);

XML

xml
<!-- String selector with static candidates -->
<selector default-value="Beta">
    <candidate>Alpha</candidate>
    <candidate>Beta</candidate>
    <candidate>Gamma</candidate>
</selector>
XML AttributeTypeDescription
default-valuestringPre-selects a candidate by its string value.
XML ChildDescription
&lt;candidate&gt;Adds a string candidate to the list. The element's text content is used as the value.

Internal Structure

IndexFieldTypeCSS classDescription
0displayUIElementThe main display bar (contains preview and buttonIcon).

The following children are nested inside the internal structure and can be targeted via CSS classes:

FieldCSS classDescription
preview.__selector_preview__Shows the currently-selected item's UI.
buttonIcon.__selector_button-icon__The dropdown arrow icon on the right.
dialog.__selector_dialog__The floating dropdown panel (attached to root when open).
listView.__selector_list-view__Direct list used when count ≤ max-item.
scrollerView.__selector_scroller-view__Scrollable list used when count > max-item.

Selector Style

SelectorStyle controls the dropdown overlay appearance and behavior.

INFO

focus-overlay

Texture drawn over the selector bar when it is hovered or focused.

Default: Sprites.RECT_RD_T_SOLID

java
selector.selectorStyle(style -> style.focusOverlay(myHighlight));

INFO

max-item

Maximum number of items rendered in the flat listView. When the candidate list is longer, a ScrollerView is used instead.

Default: 5

java
selector.selectorStyle(style -> style.maxItemCount(8));

INFO

view-height

Height of the ScrollerView when candidate count exceeds max-item.

Default: 50

java
selector.selectorStyle(style -> style.scrollerViewHeight(80f));

INFO

show-overlay

Whether hovered and selected candidates are highlighted with a semi-transparent overlay.

Default: true

java
selector.selectorStyle(style -> style.showOverlay(false));

INFO

close-after-select

Whether the dropdown closes automatically after the player selects an item.

Default: true

java
selector.selectorStyle(style -> style.closeAfterSelect(false));

Value Binding

Selector&lt;T&gt; extends BindableUIElement&lt;T&gt;, so it supports the standard data-binding API:

java
selector.bind(DataBindingBuilder.string(
    () -> config.getMode(),
    mode -> config.setMode(mode)
).build());

See Data Bindings for full details.


Fields

NameTypeAccessDescription
displayUIElementpublic finalThe main display bar.
previewUIElementpublic finalShows the selected item's UI.
buttonIconUIElementpublic finalThe dropdown arrow icon.
dialogUIElementpublic finalThe floating dropdown panel.
listViewUIElementpublic finalFlat list used for short candidate lists.
scrollerViewScrollerViewpublic finalScrollable list used for long candidate lists.
selectorStyleSelectorStyleprivate (getter)Current selector style.
candidatesList&lt;T&gt;private (getter)Current candidate list.
valueTprivate (getter/nullable)Currently selected value.

Methods

MethodReturnsDescription
setCandidates(List&lt;T&gt;)Selector&lt;T&gt;Replaces the candidate list and rebuilds the dropdown.
setCandidateUIProvider(UIElementProvider&lt;T&gt;)Selector&lt;T&gt;Sets a custom factory for rendering each candidate.
setSelected(T)Selector&lt;T&gt;Selects a value and notifies listeners.
setOnValueChanged(Consumer&lt;T&gt;)Selector&lt;T&gt;Registers a listener for value changes.
selectorStyle(Consumer&lt;SelectorStyle&gt;)Selector&lt;T&gt;Configures SelectorStyle fluently.
show()voidOpens the dropdown.
hide()voidCloses the dropdown.
isOpen()booleanReturns true if the dropdown is currently open.
getValue()TReturns the currently selected value (may be null).

Released under the MIT License.