Skip to content

TextArea ​

Since 2.2.1
TextArea examples with scrolling, disabled, and custom-style states
The first editor scrolls long content; the other two demonstrate disabled and styled text areas.

TextArea is a multi-line text editor. Its value is a String[] — one element per line. It has built-in horizontal and vertical scrollers, a text validator for error highlighting, and full keyboard support (arrows, Home/End, PageUp/PageDown, Ctrl+←/→ word navigation, selection, copy/cut/paste, undo/redo).

The editor becomes editable when focused (clicked). Double-click to select a word.

INFO

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


Usage ​

java
var area = new TextArea();
area.setValue(new String[] { "Line 1", "Line 2", "Line 3" });
area.setLinesResponder(lines -> {
    // called on each valid edit
    System.out.println("Lines: " + Arrays.toString(lines));
});
parent.addChild(area);

XML ​

xml
<!-- Pre-populated text (lines split by \n in source) -->
<text-area>Line 1
Line 2
Line 3</text-area>

INFO

TextArea cannot have layout children added in XML — only text content.


Internal Structure ​

CSS classDescription
.__text-area_content-view__The main editing surface.
.__text-area_vertical-scroller__The vertical scroll bar (right side).
.__text-area_horizontal-scroller__The horizontal scroll bar (bottom).

Text Area Style ​

INFO

focus-overlay

​

Texture drawn over the content view when it is hovered or focused.

Default: Sprites.RECT_RD_T_SOLID

java
area.textAreaStyle(style -> style.focusOverlay(myHighlight));

INFO

font / font-size

​

Font and size used for all lines.

Defaults: vanilla default font / 9

css
text-area {
    font: "minecraft:uniform";
    font-size: 9;
}

INFO

text-color / error-color

​

Text color when valid / when the validator rejects the content.

Defaults: 0xFFFFFF / 0xFF0000

css
text-area {
    text-color: #EEEEEE;
    error-color: #FF4444;
}

INFO

cursor-color

​

Color of the blinking caret.

Default: 0xEEEEEE

css
text-area {
    cursor-color: #FFFFFF;
}

INFO

text-shadow

​

Whether to draw text with a drop shadow.

Default: true

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

INFO

placeholder

​

Ghost text shown when all lines are empty.

Default: translatable key text_field.empty

java
area.textAreaStyle(style -> style
    .placeholder(Component.literal("Enter code…"))
);

INFO

line-spacing

​

Extra pixels added between lines.

Default: 1

css
text-area {
    line-spacing: 2;
}

INFO

scroller-view-mode

​

Which scroll axes are enabled. Values: HORIZONTAL, VERTICAL, BOTH.

Default: BOTH

java
area.textAreaStyle(style -> style.viewMode(ScrollerMode.VERTICAL));

INFO

scroller-vertical-display / scroller-horizontal-display

​

Visibility policy for each scroll bar. Values: AUTO (show only when content overflows), ALWAYS, NEVER.

Default: AUTO

java
area.textAreaStyle(style -> style
    .verticalScrollDisplay(ScrollDisplay.ALWAYS)
    .horizontalScrollDisplay(ScrollDisplay.NEVER)
);

INFO

scroller-view-margin

​

Right margin of the horizontal scroller when the vertical scroller is visible.

Default: 5

css
text-area {
    scroller-view-margin: 5;
}

Value Binding ​

TextArea extends BindableUIElement&lt;String[]&gt;, so it integrates with the data-binding system:

java
area.bind(DataBindingBuilder.create(
    () -> config.getLines(),
    lines -> config.setLines(lines)
).build());

See Data Bindings for full details.


Fields ​

NameTypeAccessDescription
horizontalScrollerScroller.Horizontalpublic finalThe horizontal scroll bar.
verticalScrollerScroller.Verticalpublic finalThe vertical scroll bar.
contentViewUIElementpublic finalThe visible editing surface.
textAreaStyleTextAreaStyleprivate (getter)Current style instance.
isErrorbooleanprivate (getter)true when current lines fail the validator.
cursorLineintprivate (getter)Row of the cursor (0-indexed).
cursorColintprivate (getter)Column of the cursor (0-indexed).

Methods ​

MethodReturnsDescription
setValue(String[])TextAreaSets all lines and notifies listeners.
setValue(String[], boolean)TextAreaSets all lines; second param controls notification.
setLines(List&lt;String&gt;)TextAreaConvenience for setValue(list.toArray(...)).
setLinesResponder(Consumer&lt;String[]&gt;)TextAreaRegisters a listener called on each valid edit.
setTextValidator(Predicate&lt;String[]&gt;)TextAreaCustom validator; invalid content is shown in error-color.
setCharValidator(Predicate&lt;Character&gt;)TextAreaFilters characters before they are inserted.
textAreaStyle(Consumer&lt;TextAreaStyle&gt;)TextAreaConfigures style fluently.
getValue()String[]Returns the last validated line array.
isEditable()booleantrue when focused, active, visible and displayed.
hasSelection()booleantrue if there is an active text selection.
setCursor(int, int)voidMoves the cursor to a specific (line, col).

Released under the MIT License.