TextInput
com.jsmacrosce.wagyourgui.elements.TextInput
A native-button-backed editable text widget with selection and optional change and press callbacks.
Its constructor does not register it with a host; add it to an existing host separately for input and rendering.
js
function makeTextInput(font, onChanged) {
const TextInput = Java.type('com.jsmacrosce.wagyourgui.elements.TextInput');
return new TextInput(10, 10, 120, 20, font, Color.rgb(0x00, 0x00, 0x00), Color.rgb(0xFF, 0xFF, 0xFF),
Color.argb(0x80, 0x40, 0x40, 0xFF), Color.rgb(0xFF, 0xFF, 0xFF), '', null, JavaWrapper.methodToJava(onChanged));
}Supply a native font and JavaScript function for onChanged; onClick may be null.
Constructors
new TextInput
new TextInput(int x, int y, int width, int height, Font textRenderer, int color, int borderColor, int highlightColor, int textColor, String message, Consumer<Button> onClick, Consumer<String> onChange)#Initializes native geometry and font, stores editable content and optional callbacks, and collapses selection at the content end. The native button label starts empty;
message initializes editable content, not that label. Color, border, and text colors use button alpha correction; native hover fill uses color, while highlightColor is stored raw as the selection background. Does not invoke callbacks or register with a host.Parameters:
x: left position in GUI unitsy: top position in GUI unitswidth: width in GUI unitsheight: height in GUI unitstextRenderer: non-null native fontcolor: idle fill as packed ARGBborderColor: border as packed ARGBhighlightColor: raw packed ARGB selection backgroundtextColor: text color as packed ARGBmessage: non-null initial editable contentonClick: optional native button press callback; may be nullonChange: optional content-change callback; may be null
Fields
content
Current editable display string, initialized from the constructor message; it is not the native button label. Direct writes and
setMessage(String) do not update selection positions or other native-widget state; keep indices valid and refresh their GUI positions with the corresponding update methods after changing content.mask
onChange
Optional mutable callback receiving stored content after accepted character input, paste, cut, or deletion; null suppresses notification. Some boundary deletions notify even when content is unchanged. Constructor initialization, direct field writes,
setMessage(String), selection changes, and focus changes do not notify. The callback runs before later selection updates.selEndIndex
selEndIndex: int#Zero-based UTF-16 end endpoint of the selection, from zero through
content.length(); endpoints may be reversed. Direct writes do not refresh its cached GUI position; use updateSelEnd(int) to recalculate it.selStartIndex
selStartIndex: int#Zero-based UTF-16 start endpoint of the selection, from zero through
content.length(); endpoints may be reversed. Direct writes do not refresh its cached GUI position; use updateSelStart(int) to recalculate it.Methods
charTyped
charTyped(CharacterEvent characterEvent): boolean#Replaces the current selection with the event's Java code point only when the resulting content fully matches the mask; this method does not check focus. Inserts one or two UTF-16 code units and advances selection and arrowCursor by that count. Reversed selection endpoints are exchanged before testing, even if insertion is rejected. Accepted content is stored before the optional change callback, then selection and arrowCursor are updated; a callback failure prevents those later updates. Requires a valid non-null regex mask and valid indices in non-null content. An out-of-range code point throws IllegalArgumentException before selection changes. Does not delegate to the superclass and returns false on normal completion even when insertion was accepted.
Parameters:
characterEvent: non-null native character event; only its Java code point in0..0x10FFFFis used
Returns: false on normal completion, independently of acceptance
keyPressed
Handles focused text-editing shortcuts and navigation, then delegates the key event to the superclass. Supports select-all, copy, paste, cut, deletion and cursor navigation; paste and deletions bypass the mask. Shortcuts use the event's native edit-shortcut modifier (Command on macOS, Control elsewhere), with Shift and Alt absent. Left/right selection extension uses that modifier in Minecraft 1.21.10, but Control specifically in 1.21.11 and later, not Shift. Select-all, Home and End update selection endpoints without refreshing arrowCursor. Edits can notify the optional change callback even when deletion leaves content unchanged; a callback failure can leave changed content before endpoint updates and prevents superclass delegation.
Parameters:
keyEvent: non-null native key event; its key and modifier bits are read before the focus check and drive local handling, while its scan code is only forwarded to the superclass
Returns: the superclass's handled state, not whether text changed; local edits can occur even when it is false
mouseClicked
mouseClicked(MouseButtonEvent buttonEvent, boolean doubleClick): boolean#When already focused on entry, moves both selection endpoints and
arrowCursor to the position derived from the pointer's x-coordinate, then delegates to the native widget. The local position update occurs before superclass filtering and may happen even if the returned handled state is false; an unfocused call makes no local cursor change before delegation.Parameters:
buttonEvent: non-null pointer event; its x-coordinate in GUI units is read before the focus check, and the event is forwarded unchanged to the native handlerdoubleClick: double-click flag forwarded to the native handler, unused locally
Returns: the native handler's handled state, not whether the local selection moved
mouseDragged
mouseDragged(MouseButtonEvent buttonEvent, double deltaX, double deltaY): boolean#When focused on entry, moves the selection end and
arrowCursor to the position derived from the pointer's x-coordinate, leaving the start endpoint unchanged, then delegates to the native widget. The local update has no button, bounds, active, or visibility guard; the native return value is independent of whether that update occurred.Parameters:
buttonEvent: non-null pointer event; its x-coordinate in GUI units is read before the focus check, and the event is forwarded unchanged to the native handlerdeltaX: horizontal drag delta forwarded to the native handler, unused locallydeltaY: vertical drag delta forwarded to the native handler, unused locally
Returns: the native handler's handled state
setMessage
Replaces editable content only; unlike the inherited component-label setter, this does not update the native button label. Does not apply
mask, notify onChange, or refresh selection positions or arrowCursor; keep existing indices valid and refresh positions with the corresponding update methods.Parameters:
message: replacement content; supported as non-null
Sets the native label and caches wrapping at width minus four GUI units, a height-limited visible-line count and vertical offset for the current geometry. The cache is not refreshed by later geometry changes until this method is called again. This method does not invoke the press callback.
Parameters:
message: nonnull native component label
setSelected
setSelected(boolean sel): void#Sets this widget's native focus flag without changing text selection or arrowCursor, invoking the change callback, or requesting focus through the host.
Parameters:
sel: desired native widget focus state, not a text-selection flag
swapStartEnd
swapStartEnd(): void#Exchanges the selection's start and end indices and recalculates their GUI positions from the current content and font. This is an exchange, not a sort; it does not change content, arrowCursor, focus or the change callback. Indices must be within the current non-null content's range.
updateSelEnd
updateSelEnd(int endIndex): void#Stores the selection end endpoint and recalculates its cached GUI x-position from current content and font. Indices are zero-based UTF-16 offsets in
0..content.length(); the index is stored before position calculation, which can fail for invalid content or index.Parameters:
endIndex: nonnegative UTF-16 endpoint within current content
updateSelStart
updateSelStart(int startIndex): void#Stores the selection start endpoint and recalculates its cached GUI x-position from current content and font. Indices are zero-based UTF-16 offsets in
0..content.length(); the index is stored before position calculation, which can fail for invalid content or index.Parameters:
startIndex: nonnegative UTF-16 endpoint within current content