Class TextInput
java.lang.Object
dev.xichil.mvui.Entry
dev.xichil.mvui.TextInput
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 withinitial, 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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescription<R> Raccept(EntryVisitor<R> visitor) Calls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result.static TextInput.BuilderStarts building a text field.error()Why the current text is invalid, from the mod's validator; shown below the field.booleanisValid()Whether the current text is valid:error()isnull.label()The text shown above the field, from the mod's supplier.intThe most characters the field accepts; longer text is cut.voidStores the text (cut tomaxLength();nullcounts as empty) and passes it toonChangeif it is valid.value()The text in the field now, valid or not; nevernull.
-
Method Details
-
builder
Starts building a text field.- Parameters:
id- the field's id, unique within its screen- Returns:
- a new builder
- Throws:
IllegalArgumentException- ifidis invalid
-
label
-
maxLength
public int maxLength()The most characters the field accepts; longer text is cut.- Returns:
- the most characters the field accepts
-
value
The text in the field now, valid or not; nevernull.- Returns:
- the text in the field, valid or not
-
setValue
Stores the text (cut tomaxLength();nullcounts as empty) and passes it toonChangeif 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
Why the current text is invalid, from the mod's validator; shown below the field.- Returns:
- the error, or
nullif the text is valid
-
isValid
public boolean isValid()Whether the current text is valid:error()isnull.- Returns:
trueif the text is valid
-
accept
Description copied from class:EntryCalls theEntryVisitormethod for this entry's type (visitButtonfor aButton, …) and returns its result. ASectionvisits as its header, aNumberFieldas itsTextInput.
-