Component
, Interactable
, TextGUIElement
public class TextBox extends AbstractInteractableComponent<TextBox>
TextBox
:es, scrollbars will be automatically displayed if needed.
Size-wise, a TextBox
should be hard-coded to a particular size, it's not good at guessing how large it should
be. You can do this through the constructor.
Modifier and Type | Class | Description |
---|---|---|
static class |
TextBox.DefaultTextBoxRenderer |
This is the default text box renderer that is used if you don't override anything.
|
static class |
TextBox.Style |
Enum value to force a
TextBox to be either single line or multi line. |
static interface |
TextBox.TextBoxRenderer |
Helper interface that doesn't add any new methods but makes coding new text box renderers a little bit more clear
|
static interface |
TextBox.TextChangeListener |
Listener interface for when the
TextBox content has changed. |
Interactable.FocusChangeDirection, Interactable.Result
Constructor | Description |
---|---|
TextBox() |
Default constructor, this creates a single-line
TextBox of size 10 which is initially empty |
TextBox(TerminalSize preferredSize) |
Creates a new empty
TextBox with a specific size |
TextBox(TerminalSize preferredSize,
TextBox.Style style) |
Creates a new empty
TextBox with a specific size and style |
TextBox(TerminalSize preferredSize,
String initialContent) |
Creates a new empty
TextBox with a specific size and initial content |
TextBox(TerminalSize preferredSize,
String initialContent,
TextBox.Style style) |
Main constructor of the
TextBox which decides size, initial content and style |
TextBox(String initialContent) |
Constructor that creates a
TextBox with an initial content and attempting to be big enough to display
the whole text at once without scrollbars |
TextBox(String initialContent,
TextBox.Style style) |
Creates a
TextBox that has an initial content and attempting to be big enough to display the whole text
at once without scrollbars. |
Modifier and Type | Method | Description |
---|---|---|
TextBox |
addLine(String line) |
Adds a single line to the
TextBox at the end, this only works when in multi-line mode |
protected TextBox.TextBoxRenderer |
createDefaultRenderer() |
When you create a custom component, you need to implement this method and return a Renderer which is responsible
for taking care of sizing the component, rendering it and choosing where to place the cursor (if Interactable).
|
TerminalPosition |
getCaretPosition() |
Returns the position of the caret, as a
TerminalPosition where the row and columns equals the coordinates
in a multi-line TextBox and for single-line TextBox you can ignore the row component. |
String |
getLine(int index) |
Returns the line on the specific row.
|
int |
getLineCount() |
Returns the number of lines currently in this TextBox.
|
Character |
getMask() |
Returns the current text mask, meaning the substitute to draw instead of the text inside the
TextBox . |
TextBox.TextBoxRenderer |
getRenderer() |
Returns the renderer used to draw this component and measure its preferred size.
|
String |
getText() |
Returns the text in this
TextBox , for multi-line mode all lines will be concatenated together with \n as
separator. |
String |
getTextOrDefault(String defaultValueIfEmpty) |
Helper method, it will return the content of the
TextBox unless it's empty in which case it will return
the supplied default value |
Interactable.Result |
handleKeyStroke(KeyStroke keyStroke) |
This method can be overridden to handle various user input (mostly from the keyboard) when this component is in
focus.
|
boolean |
isCaretWarp() |
Checks whether caret warp mode is enabled or not.
|
boolean |
isHorizontalFocusSwitching() |
If
true , the TextBox will switch focus to the next available component to the left if the cursor in the
TextBox is at the left-most position (index 0) on the row and the user pressed the 'left' arrow key, or vice
versa for pressing the 'right' arrow key when the cursor in at the right-most position of the current row. |
boolean |
isReadOnly() |
Returns
true if this TextBox is in read-only mode, meaning text input from the user through the
keyboard is prevented |
boolean |
isVerticalFocusSwitching() |
If
true , the component will switch to the next available component above if the cursor is at the top of
the TextBox and the user presses the 'up' array key, or switch to the next available component below if the
cursor is at the bottom of the TextBox and the user presses the 'down' array key. |
TextBox |
removeLine(int lineIndex) |
Removes a line from a
TextBox component. |
TextBox |
setCaretPosition(int column) |
Moves the text caret position horizontally to a new position in the
TextBox . |
TextBox |
setCaretPosition(int line,
int column) |
Moves the text caret position to a new position in the
TextBox . |
TextBox |
setCaretWarp(boolean caretWarp) |
Sets if the caret should jump to the beginning of the next line if right arrow is pressed while at the end of a
line.
|
TextBox |
setHorizontalFocusSwitching(boolean horizontalFocusSwitching) |
If set to
true , the TextBox will switch focus to the next available component to the left if the cursor
in the TextBox is at the left-most position (index 0) on the row and the user pressed the 'left' arrow key, or
vice versa for pressing the 'right' arrow key when the cursor in at the right-most position of the current row. |
TextBox |
setMask(Character mask) |
Sets the current text mask, meaning the substitute to draw instead of the text inside the
TextBox . |
TextBox |
setReadOnly(boolean readOnly) |
Sets the read-only mode of the
TextBox , meaning text input from the user through the keyboard is
prevented. |
TextBox |
setText(String text) |
Updates the text content of the
TextBox to the supplied string. |
TextBox |
setTextChangeListener(TextBox.TextChangeListener textChangeListener) |
Assigns a change listener for when the TextBox content has changed.
|
TextBox |
setValidationPattern(Pattern validationPattern) |
Sets a pattern on which the content of the text box is to be validated.
|
TextBox |
setVerticalFocusSwitching(boolean verticalFocusSwitching) |
If set to
true , the component will switch to the next available component above if the cursor is at the
top of the TextBox and the user presses the 'up' array key, or switch to the next available component below if
the cursor is at the bottom of the TextBox and the user presses the 'down' array key. |
addTo, calculatePreferredSize, draw, getBasePane, getGlobalPosition, getLayoutData, getParent, getPosition, getPreferredSize, getSize, getTextGUI, getTheme, getThemeDefinition, hasParent, invalidate, isInside, isInvalid, isVisible, onAdded, onAfterDrawing, onBeforeDrawing, onRemoved, runOnGUIThreadIfExistsOtherwiseRunDirect, self, setLayoutData, setPosition, setPreferredSize, setRenderer, setSize, setTheme, setVisible, toBasePane, toGlobal, withBorder
afterEnterFocus, afterLeaveFocus, getCursorLocation, getInputFilter, handleInput, isActivationStroke, isEnabled, isFocusable, isFocused, isKeyboardActivationStroke, isMouseActivationStroke, isMouseDown, isMouseDrag, isMouseMove, isMouseUp, onEnterFocus, onLeaveFocus, setEnabled, setInputFilter, takeFocus
addTo, getBasePane, getGlobalPosition, getLayoutData, getParent, getPosition, getPreferredSize, getSize, getTextGUI, getTheme, getThemeDefinition, hasParent, invalidate, isInside, isVisible, onAdded, onRemoved, setLayoutData, setPosition, setPreferredSize, setSize, setTheme, setVisible, toBasePane, toGlobal, withBorder
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
draw, isInvalid
public TextBox()
TextBox
of size 10 which is initially emptypublic TextBox(String initialContent)
TextBox
with an initial content and attempting to be big enough to display
the whole text at once without scrollbarsinitialContent
- Initial content of the TextBox
public TextBox(String initialContent, TextBox.Style style)
TextBox
that has an initial content and attempting to be big enough to display the whole text
at once without scrollbars.initialContent
- Initial content of the TextBox
style
- Forced style instead of auto-detectingpublic TextBox(TerminalSize preferredSize)
TextBox
with a specific sizepreferredSize
- Size of the TextBox
public TextBox(TerminalSize preferredSize, TextBox.Style style)
TextBox
with a specific size and stylepreferredSize
- Size of the TextBox
style
- Style to usepublic TextBox(TerminalSize preferredSize, String initialContent)
TextBox
with a specific size and initial contentpreferredSize
- Size of the TextBox
initialContent
- Initial content of the TextBox
public TextBox(TerminalSize preferredSize, String initialContent, TextBox.Style style)
TextBox
which decides size, initial content and stylepreferredSize
- Size of the TextBox
initialContent
- Initial content of the TextBox
style
- Style to use for this TextBox
, instead of auto-detectingpublic TextBox setValidationPattern(Pattern validationPattern)
null
, all validation is turned off.validationPattern
- Pattern to validate the lines in this TextBox against, or null
to disablepublic TextBox setTextChangeListener(TextBox.TextChangeListener textChangeListener)
textChangeListener
- Text change listener to invoke when the TextBox content has changedpublic TextBox setText(String text)
TextBox
to the supplied string.text
- New text to assign to the TextBox
public TextBox.TextBoxRenderer getRenderer()
Component
getRenderer
in interface Component
getRenderer
in class AbstractInteractableComponent<TextBox>
public TextBox addLine(String line)
TextBox
at the end, this only works when in multi-line modeline
- Line to add at the end of the content in this TextBox
public TextBox removeLine(int lineIndex)
TextBox
component. If the component is single-line, they only valid call to this
method is removeLine(0)
which has the same effect as calling setText("")
. For multi-line text
boxes, the line at the specified index will be removed. Will throw ArrayIndexOutOfBoundsException
if you
specified an incorrect index.lineIndex
- Index of the line to remove, has to be 0 or greater and less than the number of lines in the
text boxpublic TextBox setCaretWarp(boolean caretWarp)
false
.caretWarp
- Whether the caret will warp at the beginning/end of linespublic boolean isCaretWarp()
setCaretWarp
for more details.true
if caret warp mode is enabledpublic TerminalPosition getCaretPosition()
TerminalPosition
where the row and columns equals the coordinates
in a multi-line TextBox
and for single-line TextBox
you can ignore the row
component.public TextBox setCaretPosition(int column)
public TextBox setCaretPosition(int line, int column)
public String getText()
TextBox
, for multi-line mode all lines will be concatenated together with \n as
separator.TextBox
public String getTextOrDefault(String defaultValueIfEmpty)
TextBox
unless it's empty in which case it will return
the supplied default valuedefaultValueIfEmpty
- Value to return if the TextBox
is emptyTextBox
or defaultValueIfEmpty
is the TextBox
is emptypublic Character getMask()
TextBox
.
This is normally used for password input fields so the password isn't shownnull
if there is no maskpublic TextBox setMask(Character mask)
TextBox
.
This is normally used for password input fields so the password isn't shownmask
- New text mask or null
if there is no maskpublic boolean isReadOnly()
true
if this TextBox
is in read-only mode, meaning text input from the user through the
keyboard is preventedtrue
if this TextBox
is in read-only modepublic TextBox setReadOnly(boolean readOnly)
TextBox
, meaning text input from the user through the keyboard is
prevented. The user can still focus and scroll through the text in this mode.readOnly
- If true
then the TextBox
will switch to read-only modepublic boolean isVerticalFocusSwitching()
true
, the component will switch to the next available component above if the cursor is at the top of
the TextBox and the user presses the 'up' array key, or switch to the next available component below if the
cursor is at the bottom of the TextBox and the user presses the 'down' array key. The means that for single-line
TextBox:es, pressing up and down will always switch focus.true
if vertical focus switching is enabledpublic TextBox setVerticalFocusSwitching(boolean verticalFocusSwitching)
true
, the component will switch to the next available component above if the cursor is at the
top of the TextBox and the user presses the 'up' array key, or switch to the next available component below if
the cursor is at the bottom of the TextBox and the user presses the 'down' array key. The means that for
single-line TextBox:es, pressing up and down will always switch focus with this mode enabled.verticalFocusSwitching
- If called with true, vertical focus switching will be enabledpublic boolean isHorizontalFocusSwitching()
true
, the TextBox will switch focus to the next available component to the left if the cursor in the
TextBox is at the left-most position (index 0) on the row and the user pressed the 'left' arrow key, or vice
versa for pressing the 'right' arrow key when the cursor in at the right-most position of the current row.true
if horizontal focus switching is enabledpublic TextBox setHorizontalFocusSwitching(boolean horizontalFocusSwitching)
true
, the TextBox will switch focus to the next available component to the left if the cursor
in the TextBox is at the left-most position (index 0) on the row and the user pressed the 'left' arrow key, or
vice versa for pressing the 'right' arrow key when the cursor in at the right-most position of the current row.horizontalFocusSwitching
- If called with true, horizontal focus switching will be enabledpublic String getLine(int index)
getText()
. If the row index is invalid (less than zero or equals or larger than the
number of rows), this method will throw IndexOutOfBoundsException.index
- Index of the row to return the contents fromIndexOutOfBoundsException
- if the row index is less than zero or too largepublic int getLineCount()
protected TextBox.TextBoxRenderer createDefaultRenderer()
AbstractComponent
createDefaultRenderer
in class AbstractInteractableComponent<TextBox>
public Interactable.Result handleKeyStroke(KeyStroke keyStroke)
AbstractInteractableComponent
handleInput(..)
is final in
AbstractInteractableComponent
to ensure the input filter is properly handled. If the filter decides that
this event should be processed, it will call this method.handleKeyStroke
in class AbstractInteractableComponent<TextBox>
keyStroke
- What input was entered by the userCopyright © 2020. All rights reserved.