Class SelectList

java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.SelectList

public final class SelectList extends Entry
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 that is re-read on every refresh, so the list follows the mod's data; the selection lives in the entry and is identified by row id.
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();

Required: rows. Row ids must be unique within the list; a duplicate throws when the rows are read. When the selected row disappears from the rows, the selection is cleared (without calling onSelect).

  • Method Details

    • builder

      public static SelectList.Builder builder(String id)
      Starts building a list.
      Parameters:
      id - the list's id, unique within its screen
      Returns:
      a new builder
      Throws:
      IllegalArgumentException - if id is invalid
    • rows

      public List<SelectList.Row> rows()
      The current rows, from the mod's supplier. Drops the selection if its row is gone.
      Returns:
      an unmodifiable copy of the rows
      Throws:
      IllegalStateException - if two rows have the same id
    • selected

      public String selected()
      The selected row's id, or null if no row is selected. Mods read it from actions, e.g. a "Load" button that loads the selected profile.
      Returns:
      the selected row's id, or null if no row is selected
    • select

      public void select(String rowId)
      Selects a row and notifies onSelect, as a click on the row does. A mod may call it from an action, e.g. select(null) after deleting the selected item.
      Parameters:
      rowId - a row id, or null to clear the selection
    • accept

      public <R> R accept(EntryVisitor<R> visitor)
      Description copied from class: Entry
      Calls the EntryVisitor method for this entry's type (visitButton for a Button, …) and returns its result. A Section visits as its header, a NumberField as its TextInput.
      Specified by:
      accept in class Entry
      Type Parameters:
      R - the visitor's result type
      Parameters:
      visitor - the visitor, usually a backend's widget factory
      Returns:
      what the visitor returned