Skip to content

TextField

Since 2.2.1

TextField is a single-line text input element. It supports multiple input modes (free string, number, resource location, compound NBT tag), drag-to-change values for numeric fields, a text validator for highlighting invalid input, a formatter for custom display, and undo/redo via Ctrl+Z / Ctrl+Y.

The field becomes editable when it is focused (clicked). Keyboard shortcuts follow standard Minecraft conventions: Ctrl+A select all, Ctrl+C/X/V copy/cut/paste, Ctrl+←/→ word navigation, Home/End.

INFO

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


Usage

java
// Free string input
var field = new TextField();
field.setTextResponder(text -> System.out.println("Typed: " + text));
parent.addChild(field);

// Integer input with a range
var intField = new TextField();
intField.setNumbersOnlyInt(0, 100);
intField.setText("50");
intField.setTextResponder(raw -> config.setValue(Integer.parseInt(raw)));

// Resource location input
var rlField = new TextField();
rlField.setResourceLocationOnly();

XML

xml
<!-- Free string -->
<text-field value="default text"/>

<!-- Integer mode, range 0–100 -->
<text-field mode="NUMBER_INT" value="50"/>

<!-- Resource location mode -->
<text-field mode="RESOURCE_LOCATION"/>

<!-- String with a regex validator -->
<text-field mode="STRING" regex-validator="^[a-zA-Z]+$"/>
XML AttributeTypeDescription
modeModeInput mode. One of: STRING, COMPOUND_TAG, RESOURCE_LOCATION, NUMBER_INT, NUMBER_LONG, NUMBER_FLOAT, NUMBER_DOUBLE, NUMBER_SHORT, NUMBER_BYTE.
regex-validatorstringRegex pattern used to validate the text (only applied when mode="STRING").
valuestringInitial text value.

Modes

ModeDescription
STRINGAccepts any printable characters. Optional regex validator.
COMPOUND_TAGValidates that the text is a valid NBT compound tag.
RESOURCE_LOCATIONRestricts characters to valid resource location format (namespace:path).
NUMBER_INTInteger in a given [min, max] range. Supports drag and mouse-wheel to change value.
NUMBER_LONGLong integer in a given range.
NUMBER_FLOATFloat in a given range.
NUMBER_DOUBLEDouble in a given range.
NUMBER_SHORTShort integer in a given range.
NUMBER_BYTEByte in a given range.

Number fields support:

  • Mouse wheel to increment / decrement by wheelDur.
  • Click-drag horizontally to slide the value smoothly.
  • Shift modifier to multiply the step by 10.

Text Field Style

INFO

focus-overlay

Texture drawn over the field when it is hovered or focused.

Default: Sprites.RECT_RD_T_SOLID

java
field.textFieldStyle(style -> style.focusOverlay(myHighlight));

INFO

font / font-size

Font and size of the rendered text.

Defaults: vanilla default font / 9

java
field.textFieldStyle(style -> style
    .font(ResourceLocation.parse("minecraft:uniform"))
    .fontSize(10)
);

INFO

text-color / error-color

Color of valid text and of text that fails validation.

Defaults: 0xFFFFFF (white) / 0xFF0000 (red)

java
field.textFieldStyle(style -> style
    .textColor(0xFFFFFF)
    .errorColor(0xFF4444)
);

INFO

cursor-color

Color of the blinking cursor.

Default: 0xEEEEEE

java
field.textFieldStyle(style -> style.cursorColor(0xFFFFFF));

INFO

text-shadow

Whether to draw a drop shadow under the text.

Default: true

css
text-field {
    text-shadow: false;
}

INFO

placeholder

Ghost text shown when the field is empty.

Default: translatable key text_field.empty

java
field.textFieldStyle(style -> style
    .placeholder(Component.literal("Enter value…"))
);

Value Binding

TextField extends BindableUIElement&lt;String&gt;, so it supports the standard data-binding system:

java
field.bind(DataBindingBuilder.string(
    () -> config.getName(),
    name -> config.setName(name)
).build());

See Data Bindings for full details.


Fields

NameTypeAccessDescription
textFieldStyleTextFieldStyleprivate (getter)Current style instance.
textStringprivate (getter)Last validated text value.
rawTextStringprivate (getter)Current display text (may not have passed validation).
modeModeprivate (getter)Current input mode.
isErrorbooleanprivate (getter)true when rawText fails the validator.
formatterFunction&lt;String, Component&gt;private (getter/setter/nullable)Optional function to render rawText as a formatted Component.
wheelDurfloatprivate (getter)Step size for mouse-wheel / drag on number fields.
cursorPosintprivate (getter)Current cursor position in rawText.
selectionStartintprivate (getter)Selection start in rawText.
selectionEndintprivate (getter)Selection end in rawText.

Methods

MethodReturnsDescription
setText(String)TextFieldSets the value and notifies listeners.
setAnyString()TextFieldSwitches to free-string mode (no validation).
setCompoundTagOnly()TextFieldSwitches to compound-tag validation mode.
setResourceLocationOnly()TextFieldRestricts input to valid resource location characters.
setNumbersOnlyInt(int, int)TextFieldInteger mode with [min, max] range.
setNumbersOnlyLong(long, long)TextFieldLong integer mode with [min, max] range.
setNumbersOnlyFloat(float, float)TextFieldFloat mode with [min, max] range.
setNumbersOnlyDouble(double, double)TextFieldDouble mode with [min, max] range.
setNumbersOnlyShort(short, short)TextFieldShort integer mode with [min, max] range.
setNumbersOnlyByte(byte, byte)TextFieldByte integer mode with [min, max] range.
setTextValidator(Predicate&lt;String&gt;)TextFieldCustom validator; invalid text is shown in error-color.
setTextRegexValidator(String)TextFieldConvenience to compile a regex and use it as the validator.
setCharValidator(Predicate&lt;Character&gt;)TextFieldFilters characters before they are inserted.
setTextResponder(Consumer&lt;String&gt;)TextFieldRegisters a listener called on each valid text change.
setWheelDur(float)TextFieldSets the scroll/drag step for number fields.
textFieldStyle(Consumer&lt;TextFieldStyle&gt;)TextFieldConfigures style fluently.
getValue()StringReturns the last validated text.
getRawText()StringReturns the current display text (may fail validation).
isEditable()booleantrue when focused, active, visible and displayed.
isError()booleantrue when rawText does not pass validation.

Released under the MIT License.