Class TextFieldComponent

All Implemented Interfaces:
IRenderable, Tweenable, MouseListener, MouseMotionListener, MouseWheelListener, EventListener
Direct Known Subclasses:
Spinner

public class TextFieldComponent extends ImageComponent
A single-line text input GUI component. Supports input filtering through a regex format, a maximum length, a flickering caret while focused, and confirmation listeners that fire when the user presses ENTER/ESC or clicks outside the component.
  • Field Details

    • DOUBLE_FORMAT

      public static final String DOUBLE_FORMAT
      Regex matching signed decimal numbers including scientific notation.
      See Also:
    • INTEGER_FORMAT

      public static final String INTEGER_FORMAT
      Regex matching positive integers up to 10 digits.
      See Also:
  • Constructor Details

    • TextFieldComponent

      public TextFieldComponent(double x, double y, double width, double height, String text)
      Constructs a new TextFieldComponent.
      Parameters:
      x - the x coordinate of the component
      y - the y coordinate of the component
      width - the width of the component
      height - the height of the component
      text - the initial text content
  • Method Details

    • getFormat

      public String getFormat()
      Gets the regex pattern used to filter typed input.
      Returns:
      the format pattern, or null if no filter is set
    • setFormat

      public void setFormat(String format)
      Sets the regex pattern used to filter typed input.
      Parameters:
      format - the format pattern, or null to disable filtering
    • getCursor

      public String getCursor()
      Gets the string rendered as the caret while the field is focused.
      Returns:
      the caret string
    • setCursor

      public void setCursor(String newCursor)
      Sets the string rendered as the caret while the field is focused.
      Parameters:
      newCursor - the caret string
    • getMaxLength

      public int getMaxLength()
      Gets the maximum number of characters allowed in the field, or 0 if unlimited.
      Returns:
      the maximum length
    • setMaxLength

      public void setMaxLength(int maxLength)
      Sets the maximum number of characters allowed in the field.
      Parameters:
      maxLength - the maximum length, or 0 for unlimited
    • getText

      public String getText()
      Description copied from class: GuiComponent
      Gets the entire Text associated with this GuiComponent. Parts of the Text may get cropped and can therefore be invisible. To retrieve only the visible part of the text, use GuiComponent.getTextToRender(Graphics2D g).
      Overrides:
      getText in class GuiComponent
      Returns:
      the entire text on this GuiComponent
    • setText

      public void setText(String text)
      Description copied from class: GuiComponent
      Sets the text.
      Overrides:
      setText in class GuiComponent
      Parameters:
      text - the new text
    • getTextToRender

      public String getTextToRender(Graphics2D g)
      Description copied from class: GuiComponent
      Gets only the non-cropped bits of Text visible on this GuiComponent.m To retrieve only the entire text associated with this GuiComponent, use GuiComponent.getText().
      Overrides:
      getTextToRender in class GuiComponent
      Parameters:
      g - The graphics object to render on.
      Returns:
      the text to render
    • handleTypedKey

      public void handleTypedKey(KeyEvent event)
      Handles a key typed event by dispatching to the relevant editing routine (backspace, accept, or normal typing).
      Parameters:
      event - the key event
    • canHandleInput

      public boolean canHandleInput()
      Returns whether this component is currently in a state in which it should accept user input.
      Returns:
      true if input can be handled
    • onChangeConfirmed

      public void onChangeConfirmed(Consumer<String> cons)
      Registers a callback that is invoked with the current text whenever the user confirms the input (by pressing ENTER/ESC or clicking outside the component).
      Parameters:
      cons - the consumer to register
    • render

      public void render(Graphics2D g)
      Description copied from class: ImageComponent
      Renders the component using the provided Graphics2D context. If the component is suspended or not visible, the method returns immediately. Otherwise, it renders the background image (if available) and the main image (if available), and then calls the superclass's render method.
      Specified by:
      render in interface IRenderable
      Overrides:
      render in class ImageComponent
      Parameters:
      g - The Graphics2D context used for rendering.
      See Also:
    • setFlickerDelay

      public void setFlickerDelay(int flickerDelayMillis)
      Sets the delay (in milliseconds) between caret flicker toggles.
      Parameters:
      flickerDelayMillis - the flicker delay