Skip to content

TextFieldWidgetHelper ​

com.jsmacrosce.jsmacros.client.api.helper.screen.TextFieldWidgetHelper

Since1.0.5

Wraps the game's text field with a text-change callback.

The callback receives accepted new text and is attached when the field is built.
Scripted setText(String) changes invoke it when accepted.

js
const screen = Hud.createScreen("a screen", true);  
screen.setOnInit(JavaWrapper.methodToJava(function (self) {
  const field = self.textFieldBuilder()
     .pos(10, 20)
     .size(120, 14)
     .message("type a name")
     .action(JavaWrapper.methodToJava(function (text) {
       // the callback is given the text, not the field
       Chat.log(`so far: ${text}`);  
     }))
     .build();  

  // Accepted scripted changes invoke the callback too.  
  field.setText("hello");  
  // On 26.1+, rejected text restores the last accepted value, cursor, and selection.  
  field.setTextPredicate(JavaWrapper.methodToJava(function (text) {
    return 8 >= String(text).length;  
  }));  
  field.setText("far too long to be allowed");  
  Chat.log(`the field still says ${field.getText()}`);  
}));  
Hud.openScreen(screen);

Available in scripts as Widgets.TextField.

Nested types ​

Constructors ​

new TextFieldWidgetHelper ​

new TextFieldWidgetHelper(EditBox t)#
Wraps an existing text field with a helper whose drawing-order index is zero.

Parameters:

  • t: the text field to wrap
new TextFieldWidgetHelper(EditBox t, int zIndex)#
Wraps an existing text field with the specified helper drawing-order index.

Parameters:

  • t: the text field to wrap
  • zIndex: the helper's drawing-order index

Methods ​

getMaxLength ​

getMaxLength(): intSince1.8.4#
Returns the maximum number of UTF-16 code units allowed in the field; a limit of zero allows no text.
Returns: the maximum number of UTF-16 code units allowed in this text field.

getSelectedText ​

getSelectedText(): StringSince1.8.4#
Returns the selected portion of the text, or an empty string when nothing is selected.
Returns: the selected text.

getText ​

getText(): StringSince1.0.5#
Returns the entered text, excluding the field's suggestion.
Returns: the current text; empty when no text is entered.

isEditable ​

isEditable(): booleanSince1.8.4#
Returns: true if the text field is editable, false otherwise.

resetTextPredicate ​

resetTextPredicate(): TextFieldWidgetHelperSince1.8.4#
Removes the text filter, allowing all non-null text. Existing text is retained.
Returns: self for chaining.

setCursorPosition ​

setCursorPosition(int position): TextFieldWidgetHelperSince1.8.4#
Moves the cursor to a character offset and drops any selection; equivalent to calling setCursorPosition(int, boolean) with false.

Parameters:

  • position: the cursor position
Returns: self for chaining.
setCursorPosition(int position, boolean shift): TextFieldWidgetHelperSince1.9.0#
Moves the cursor to a character offset. With shift enabled, the selection is extended or shrunk to that position; otherwise it is dropped.

Parameters:

  • position: the cursor position
  • shift: whether to keep the selection and extend it to the new position
Returns: self for chaining.

setCursorToEnd ​

setCursorToEnd(): TextFieldWidgetHelperSince1.8.4#
Moves the cursor to the end of the text and drops the selection.
Returns: self for chaining.
setCursorToEnd(boolean shift): TextFieldWidgetHelperSince1.9.0#
Moves the cursor to the end of the text. With shift enabled, the existing selection is extended or shrunk to the end; otherwise it is dropped.

Parameters:

  • shift: whether to keep the selection and extend it to the new position
Returns: self for chaining.

setCursorToStart ​

setCursorToStart(): TextFieldWidgetHelperSince1.8.4#
Moves the cursor to the start of the text and drops the selection.
Returns: self for chaining.
setCursorToStart(boolean shift): TextFieldWidgetHelperSince1.9.0#
Moves the cursor to the start of the text. With shift enabled, the existing selection is extended or shrunk to the start; otherwise it is dropped.

Parameters:

  • shift: whether to keep the selection and extend it to the new position
Returns: self for chaining.

setEditable ​

setEditable(boolean edit): TextFieldWidgetHelperSince1.0.5#
Sets whether the field accepts text input. When false, it uses the uneditable text color.

Parameters:

  • edit: whether the field should be editable
Returns: self for chaining.

setEditableColor ​

setEditableColor(int color): TextFieldWidgetHelperSince1.0.5#
Sets the color used to draw text while the field is editable.

Parameters:

  • color: the packed rgb colour to draw editable text in
Returns: self for chaining.

setMaxLength ​

setMaxLength(int length): TextFieldWidgetHelperSince1.8.4#
Sets the maximum number of UTF-16 code units allowed. If the current text exceeds the new limit, it is truncated immediately; a limit of zero allows no text.

Parameters:

  • length: the new maximum number of UTF-16 code units
Returns: self for chaining.

setSelection ​

setSelection(int start, int end): TextFieldWidgetHelperSince1.8.4#
Selects text between two character offsets: start is the cursor position and end is the other selection endpoint. Offsets are passed to the game without validation.

Parameters:

  • start: the offset of the cursor
  • end: the offset of the other end of the selection
Returns: self for chaining.

setSuggestion ​

setSuggestion(String suggestion): TextFieldWidgetHelperSince1.8.4#
Sets the hint drawn in the field while it is empty; this does not change the text returned by getText().

Parameters:

  • suggestion: the suggestion to set
Returns: self for chaining.

setText ​

setText(String text): TextFieldWidgetHelperSince1.0.5#
Sets the field's text and waits for the change to be applied. Any configured text predicate applies, and the callback runs if the text is accepted.

Parameters:

  • text: the text to put in the field
Returns: self for chaining.
setText(String text, boolean await): TextFieldWidgetHelperSince1.3.1#
Sets the field's text, optionally waiting for the change to be applied. When called from the client thread there is nothing to hand off and nothing to wait for, so the await flag makes no difference there. Off the client thread the change is queued onto it, and the flag decides whether this call blocks until its own task has run. Blocking also waits out whatever else was queued ahead of it. Any configured text predicate applies to scripted changes; the callback runs for accepted changes.

Parameters:

  • text: the text to put in the field
  • await: whether to wait for the change to be applied
Returns: self for chaining.

setTextPredicate ​

setTextPredicate(MethodWrapper<String, ?, ?, ?> predicate): TextFieldWidgetHelperSince1.8.4#
Sets a predicate over the complete proposed text. On 26.1+, rejected input restores the last accepted text, cursor, and selection without running the change callback; this is enforced by the responder because EditBox has no filter. Earlier versions use the field's native filter.

Parameters:

  • predicate: the text filter
Returns: self for chaining.

setUneditableColor ​

setUneditableColor(int color): TextFieldWidgetHelperSince1.0.5#
Sets the color used to draw text while the field is not editable.

Parameters:

  • color: the packed rgb colour to draw uneditable text in
Returns: self for chaining.

toString ​

toString(): String#
Returns a diagnostic string containing the current text, without JSON escaping.
Returns: the diagnostic string, formatted as TextFieldWidgetHelper:{"text": "%s"}