Class TextInput

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

public final class TextInput extends Entry
A one-line text field with a label above it and an error line below it, like the name field of vanilla's world-creation screen.
TextInput name = TextInput.builder("name")
        .label(Text.translatable("mymod.name", "Profile name"))
        .initial(config.profileName)
        .maxLength(32)
        .validator(s -> s.trim().isEmpty() ? Text.translatable("mymod.name.empty", "Enter a name") : null)
        .onChange(s -> config.profileName = s)
        .build();

Where the text lives

Unlike most entries, the typed text lives in the entry, not in the mod: the field starts with initial, and a refresh doesn't reset it. Backends call setValue(String) on every edit and show error() below the field. onChange receives each edit only while the text is valid, so the mod never sees invalid text; an action can also read value() directly (e.g. a "Create" button that is enabled only while name.isValid()).

Required: label. Everything else has a default: empty initial text, 64 characters, no validation, no onChange. For numbers, use NumberField; for several lines, MultiLineInput.

  • Method Details

    • builder

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

      public Text label()
      The text shown above the field, from the mod's supplier.
      Returns:
      the label
    • maxLength

      public int maxLength()
      The most characters the field accepts; longer text is cut.
      Returns:
      the most characters the field accepts
    • value

      public String value()
      The text in the field now, valid or not; never null.
      Returns:
      the text in the field, valid or not
    • setValue

      public void setValue(String newValue)
      Stores the text (cut to maxLength(); null counts as empty) and passes it to onChange if it is valid. Backends call it on every edit; a mod may call it to replace the text, e.g. to clear the field after using its content.
      Parameters:
      newValue - the new text
    • error

      public Text error()
      Why the current text is invalid, from the mod's validator; shown below the field.
      Returns:
      the error, or null if the text is valid
    • isValid

      public boolean isValid()
      Whether the current text is valid: error() is null.
      Returns:
      true if the text is valid
    • 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