- java.lang.Object
-
- com.googlecode.lanterna.gui2.AbstractComponent<T>
-
- com.googlecode.lanterna.gui2.AbstractInteractableComponent<Button>
-
- com.googlecode.lanterna.gui2.Button
-
- 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 usingaddListener(Listener). To remove a previously attached action, useremoveListener(Listener).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classButton.BorderedButtonRendererstatic interfaceButton.ButtonRendererHelper interface that doesn't add any new methods but makes coding new button renderers a little bit more clearstatic classButton.DefaultButtonRendererThis is the default button renderer that is used if you don't override anything.static classButton.FlatButtonRendererAlternative button renderer that displays buttons with just the label and minimal decorationstatic interfaceButton.ListenerListener interface that can be used to catch user events on the button-
Nested classes/interfaces inherited from interface com.googlecode.lanterna.gui2.Interactable
Interactable.FocusChangeDirection, Interactable.Result
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringlabelprivate java.util.List<Button.Listener>listeners
-
Constructor Summary
Constructors Constructor Description Button(java.lang.String label)Creates a new button with a specific label and no initially attached action.Button(java.lang.String label, java.lang.Runnable action)Creates a new button with a label and an associated action to fire when triggered by the user
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddListener(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.ButtonRenderercreateDefaultRenderer()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).TerminalPositiongetCursorLocation()Returns, in local coordinates, where to put the cursor on the screen when this component has focus.java.lang.StringgetLabel()Returns the label current assigned to the buttonInteractable.ResulthandleKeyStroke(KeyStroke keyStroke)This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.booleanremoveListener(Button.Listener listener)Removes a listener from the button's list of listeners to call when the button is triggered.voidsetLabel(java.lang.String label)Updates the label on the button to the specified stringjava.lang.StringtoString()protected voidtriggerActions()-
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 Detail
-
listeners
private final java.util.List<Button.Listener> listeners
-
label
private java.lang.String label
-
-
Constructor Detail
-
Button
public Button(java.lang.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(java.lang.String label, java.lang.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 buttonaction- Action to fire when the user triggers the button by pressing the enter or the space key
-
-
Method Detail
-
createDefaultRenderer
protected Button.ButtonRenderer createDefaultRenderer()
Description copied from class:AbstractComponentWhen 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:
createDefaultRendererin classAbstractInteractableComponent<Button>- Returns:
- Renderer to use when sizing and drawing this component
-
getCursorLocation
public TerminalPosition getCursorLocation()
Description copied from interface:InteractableReturns, 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:
getCursorLocationin interfaceInteractable- Overrides:
getCursorLocationin classAbstractInteractableComponent<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:AbstractInteractableComponentThis 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 inAbstractInteractableComponentto ensure the input filter is properly handled. If the filter decides that this event should be processed, it will call this method.- Overrides:
handleKeyStrokein classAbstractInteractableComponent<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(java.lang.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:
trueif this button contained the specified listener
-
getLabel
public java.lang.String getLabel()
Returns the label current assigned to the button- Returns:
- Label currently used by the button
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-