java.lang.Object
com.googlecode.lanterna.gui2.AbstractComponent<Button>
com.googlecode.lanterna.gui2.AbstractInteractableComponent<Button>
com.googlecode.lanterna.gui2.Button
- All Implemented Interfaces:
Component
,Interactable
,TextGUIElement
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)
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static interface
Helper interface that doesn't add any new methods but makes coding new button renderers a little bit more clearstatic class
This is the default button renderer that is used if you don't override anything.static class
Alternative button renderer that displays buttons with just the label and minimal decorationstatic interface
Listener interface that can be used to catch user events on the buttonNested classes/interfaces inherited from interface com.googlecode.lanterna.gui2.Interactable
Interactable.FocusChangeDirection, Interactable.Result
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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 addedprotected Button.ButtonRenderer
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).Returns, in local coordinates, where to put the cursor on the screen when this component has focus.getLabel()
Returns the label current assigned to the buttonhandleKeyStroke
(KeyStroke keyStroke) This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.boolean
removeListener
(Button.Listener listener) Removes a listener from the button's list of listeners to call when the button is triggered.final void
Updates the label on the button to the specified stringtoString()
protected void
Methods inherited from class com.googlecode.lanterna.gui2.AbstractInteractableComponent
afterEnterFocus, afterLeaveFocus, getInputFilter, getRenderer, handleInput, isActivationStroke, isEnabled, isFocusable, isFocused, isKeyboardActivationStroke, isMouseActivationStroke, isMouseDown, isMouseDrag, isMouseMove, isMouseUp, onEnterFocus, onLeaveFocus, setEnabled, setInputFilter, takeFocus
Methods inherited from class com.googlecode.lanterna.gui2.AbstractComponent
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
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.googlecode.lanterna.gui2.Component
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
Methods inherited from interface com.googlecode.lanterna.gui2.TextGUIElement
draw, isInvalid
-
Field Details
-
listeners
-
label
-
-
Constructor Details
-
Button
Creates a new button with a specific label and no initially attached action.- Parameters:
label
- Label to put on the button
-
Button
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 buttonaction
- Action to fire when the user triggers the button by pressing the enter or the space key
-
-
Method Details
-
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 classAbstractInteractableComponent<Button>
- Returns:
- Renderer to use when sizing and drawing this component
-
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 interfaceInteractable
- Overrides:
getCursorLocation
in classAbstractInteractableComponent<Button>
- Returns:
- Coordinates of where to place the cursor when this component has focus
-
handleKeyStroke
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 inAbstractInteractableComponent
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 classAbstractInteractableComponent<Button>
- Parameters:
keyStroke
- What input was entered by the user- Returns:
- Result of processing the key-stroke
-
triggerActions
protected void triggerActions() -
setLabel
Updates the label on the button to the specified string- Parameters:
label
- New label to use on the button
-
addListener
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
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
Returns the label current assigned to the button- Returns:
- Label currently used by the button
-
toString
-