SelectList
SelectList is a scrolling list of rows with at most one selected, like the world list, for picking an item that buttons then act on.
A scrolling list of rows of text, at most one of them selected, like vanilla's world or server lists. The rows come from a supplier, so the list follows your data; the selection lives in the entry.
SelectList profiles = SelectList.builder("profiles")
.rows(() -> {
List<SelectList.Row> rows = new ArrayList<>();
for (Profile p : store.profiles()) {
rows.add(SelectList.Row.of(p.id(), Text.literal(p.name())));
}
return rows;
})
.build();
Button delete = Button.builder("delete")
.label(Text.translatable("mymod.delete", "Delete"))
.enabled(() -> profiles.selected() != null)
.onPress(() -> store.delete(profiles.selected()))
.build();
Builder#
| Method | Required | Default | Notes |
|---|---|---|---|
rows(Supplier<List<SelectList.Row>>) |
yes | the rows in display order; re-read on every refresh. Return a fresh list each time. | |
onSelect(Consumer<String>) |
no | does nothing | called with the row id when the player selects a row; main thread |
enabled, tooltip, visible |
no | options every widget has |
SelectList.Row.of(id, text) makes a row: id is how your code knows it, text is what is shown.
Methods#
| Method | Does |
|---|---|
selected() |
the selected row's id, or null |
select(rowId) |
selects a row and calls onSelect, as a click does; select(null) clears the selection |
rows() |
the current rows |
Behaviour#
- Nothing is selected at first.
- When the selected row disappears from the rows, the selection is cleared, without calling
onSelect. - Row ids must be unique within the list. A duplicate throws when the rows are read, and since that happens in your supplier, it turns your menu off (Failure isolation). Use stable, unique ids such as database keys or file names.
- Actions usually read
selected()rather than trackingonSelect. - Footer: yes.
Test it#
host.select("profiles", "p1");
host.click("delete");
Rendered as [list profiles] followed by one line per row, > p1: Default for the selected row.
See also#
- Javadoc: SelectList, SelectList.Row
- A list manager