All Implemented Interfaces:
Component, Interactable, TextGUIElement

public class Button extends AbstractInteractableComponent<Button>
Simple labeled button that the user can trigger by pressing the Enter or the Spacebar key on the keyboard when the component is in focus. You can specify an initial action through one of the constructors and you can also add additional actions to the button using addListener(Listener). To remove a previously attached action, use removeListener(Listener).
  • Field Details

  • Constructor Details

    • Button

      public Button(String label)
      Creates a new button with a specific label and no initially attached action.
      Parameters:
      label - Label to put on the button
    • Button

      public Button(String label, Runnable action)
      Creates a new button with a label and an associated action to fire when triggered by the user
      Parameters:
      label - Label to put on the button
      action - Action to fire when the user triggers the button by pressing the enter or the space key
  • Method Details

    • createDefaultRenderer

      protected Button.ButtonRenderer createDefaultRenderer()
      Description copied from class: AbstractComponent
      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). This value is intended to be overridden by custom themes.
      Specified by:
      createDefaultRenderer in class AbstractInteractableComponent<Button>
      Returns:
      Renderer to use when sizing and drawing this component
    • getCursorLocation

      public TerminalPosition getCursorLocation()
      Description copied from interface: Interactable
      Returns, in local coordinates, where to put the cursor on the screen when this component has focus. If null, the cursor should be hidden. If you component is 5x1 and you want to have the cursor in the middle (when in focus), return [2,0]. The GUI system will convert the position to global coordinates.
      Specified by:
      getCursorLocation in interface Interactable
      Overrides:
      getCursorLocation in class AbstractInteractableComponent<Button>
      Returns:
      Coordinates of where to place the cursor when this component has focus
    • handleKeyStroke

      public Interactable.Result handleKeyStroke(KeyStroke keyStroke)
      Description copied from class: AbstractInteractableComponent
      This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus. The input method from the interface, 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.
      Overrides:
      handleKeyStroke in class AbstractInteractableComponent<Button>
      Parameters:
      keyStroke - What input was entered by the user
      Returns:
      Result of processing the key-stroke
    • triggerActions

      protected void triggerActions()
    • setLabel

      public final void setLabel(String label)
      Updates the label on the button to the specified string
      Parameters:
      label - New label to use on the button
    • addListener

      public void addListener(Button.Listener listener)
      Adds a listener to notify when the button is triggered; the listeners will be called serially in the order they were added
      Parameters:
      listener - Listener to call when the button is triggered
    • removeListener

      public boolean removeListener(Button.Listener listener)
      Removes a listener from the button's list of listeners to call when the button is triggered. If the listener list doesn't contain the listener specified, this call do with do nothing.
      Parameters:
      listener - Listener to remove from this button's listener list
      Returns:
      true if this button contained the specified listener
    • getLabel

      public String getLabel()
      Returns the label current assigned to the button
      Returns:
      Label currently used by the button
    • toString

      public String toString()
      Overrides:
      toString in class Object