Module com.googlecode.lanterna
Package com.googlecode.lanterna.gui2
Class AbstractListBox<V,T extends AbstractListBox<V,T>>
java.lang.Object
com.googlecode.lanterna.gui2.AbstractComponent<T>
com.googlecode.lanterna.gui2.AbstractInteractableComponent<T>
com.googlecode.lanterna.gui2.AbstractListBox<V,T>
- Type Parameters:
V
- Type of items this list box containsT
- Should always be itself, seeAbstractComponent
- All Implemented Interfaces:
Component
,Interactable
,TextGUIElement
- Direct Known Subclasses:
ActionListBox
,CheckBoxList
,RadioBoxList
public abstract class AbstractListBox<V,T extends AbstractListBox<V,T>>
extends AbstractInteractableComponent<T>
Base class for several list box implementations, this will handle things like list of items and the scrollbar.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
AbstractListBox.DefaultListBoxRenderer<V,
T extends AbstractListBox<V, T>> The default renderer forAbstractListBox
and all its subclasses.static class
AbstractListBox.ListItemRenderer<V,
T extends AbstractListBox<V, T>> The default list item renderer class, this can be extended and customized it needed.Nested classes/interfaces inherited from interface com.googlecode.lanterna.gui2.Interactable
Interactable.FocusChangeDirection, Interactable.Result
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AbstractListBox.ListItemRenderer
<V, T> protected TerminalPosition
private int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
This constructor sets up the component so it has no preferred size but will ask to be as big as the list is.protected
AbstractListBox
(TerminalSize size) This constructor sets up the component with a preferred size that is will always request, no matter what items are in the list box. -
Method Summary
Modifier and TypeMethodDescriptionAdds one more item to the list box, at the end.protected void
afterEnterFocus
(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus) Called byAbstractInteractableComponent
automatically after this component has received input focus.Removes all items from the list boxprotected AbstractListBox.ListItemRenderer
<V, T> Method that constructs theListItemRenderer
that this list box should use to draw the elements of the list box.protected InteractableRenderer
<T> 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).protected int
getIndexByMouseAction
(MouseAction click) By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets index clicked on by mouse action.getItemAt
(int index) Retrieves the item at the specified index in the list boxint
Returns the number of items currently in the list boxgetItems()
Returns a copy of the items in the list box as aList
(package private) AbstractListBox.ListItemRenderer
<V, T> int
Returns the index of the currently selected item in the list box.Returns the currently selected item in the list box.handleKeyStroke
(KeyStroke keyStroke) This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.int
Looks for the particular item in the list and returns the index within the list (starting from zero) of that item if it is found, or -1 otherwiseboolean
isEmpty()
Checks if the list box has no itemsboolean
Returnstrue
if this interactable component is currently able to receive input focus.removeItem
(int index) Removes an item from the list box by its index.private boolean
selectByCharacter
(Character character) setListItemRenderer
(AbstractListBox.ListItemRenderer<V, T> listItemRenderer) This method overrides theListItemRenderer
that is used to draw each element in the list box.setSelectedIndex
(int index) Sets which item in the list box that is currently selected.Methods inherited from class com.googlecode.lanterna.gui2.AbstractInteractableComponent
afterLeaveFocus, getCursorLocation, getInputFilter, getRenderer, handleInput, isActivationStroke, isEnabled, 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, toString, 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
-
items
-
selectedIndex
private int selectedIndex -
listItemRenderer
-
scrollOffset
-
-
Constructor Details
-
AbstractListBox
protected AbstractListBox()This constructor sets up the component so it has no preferred size but will ask to be as big as the list is. If the GUI cannot accommodate this size, scrolling and a vertical scrollbar will be used. -
AbstractListBox
This constructor sets up the component with a preferred size that is will always request, no matter what items are in the list box. If there are more items than the size can contain, scrolling and a vertical scrollbar will be used. Calling this constructor with anull
value has the same effect as calling the default constructor.- Parameters:
size
- Preferred size that the list should be asking for instead of invoking the preferred size calculation, or if set tonull
will ask to be big enough to display all items.
-
-
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<T extends AbstractListBox<V,
T>> - Returns:
- Renderer to use when sizing and drawing this component
-
createDefaultListItemRenderer
Method that constructs theListItemRenderer
that this list box should use to draw the elements of the list box. This can be overridden to supply a custom renderer. Note that this is not the renderer used for the entire list box but for each item, called one by one.- Returns:
ListItemRenderer
to use when drawing the items in the list
-
getListItemRenderer
AbstractListBox.ListItemRenderer<V,T> getListItemRenderer() -
setListItemRenderer
This method overrides theListItemRenderer
that is used to draw each element in the list box. Note that this is not the renderer used for the entire list box but for each item, called one by one.- Parameters:
listItemRenderer
- New renderer to use when drawing the items in the list box- Returns:
- Itself
-
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<T extends AbstractListBox<V,
T>> - Parameters:
keyStroke
- What input was entered by the user- Returns:
- Result of processing the key-stroke
-
getIndexByMouseAction
By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets index clicked on by mouse action.- Returns:
- index of a item that was clicked on with
MouseAction
-
selectByCharacter
-
afterEnterFocus
protected void afterEnterFocus(Interactable.FocusChangeDirection direction, Interactable previouslyInFocus) Description copied from class:AbstractInteractableComponent
Called byAbstractInteractableComponent
automatically after this component has received input focus. You can override this method if you need to trigger some action based on this.- Overrides:
afterEnterFocus
in classAbstractInteractableComponent<T extends AbstractListBox<V,
T>> - Parameters:
direction
- How focus was transferred, keep in mind this is from the previous component's point of view so if this parameter has value DOWN, focus came in from abovepreviouslyInFocus
- Which interactable component had focus previously
-
addItem
Adds one more item to the list box, at the end.- Parameters:
item
- Item to add to the list box- Returns:
- Itself
-
removeItem
Removes an item from the list box by its index. The current selection in the list box will be adjusted accordingly.- Parameters:
index
- Index of the item to remove- Returns:
- The item that was removed
- Throws:
IndexOutOfBoundsException
- if the index is out of bounds in regards to the list of items
-
clearItems
Removes all items from the list box- Returns:
- Itself
-
isFocusable
public boolean isFocusable()Description copied from interface:Interactable
Returnstrue
if this interactable component is currently able to receive input focus. This is similar but different fromInteractable.isEnabled()
, which tells lanterna that the entire component is disabled when it is returningfalse
, compared to this method which simply claims that the component is currently not ready to handle input. TheAbstractInteractableComponent
implementation always returntrue
here but for example the list box components will override and returnfalse
here if they are empty. Note that you can still programmatically force input focus onto the component, returningfalse
here won't prevent that.- Specified by:
isFocusable
in interfaceInteractable
- Overrides:
isFocusable
in classAbstractInteractableComponent<T extends AbstractListBox<V,
T>> - Returns:
true
if this component wants to receive input focus,false
otherwise.
-
indexOf
Looks for the particular item in the list and returns the index within the list (starting from zero) of that item if it is found, or -1 otherwise- Parameters:
item
- What item to search for in the list box- Returns:
- Index of the item in the list box or -1 if the list box does not contain the item
-
getItemAt
Retrieves the item at the specified index in the list box- Parameters:
index
- Index of the item to fetch- Returns:
- The item at the specified index
- Throws:
IndexOutOfBoundsException
- If the index is less than zero or equals/greater than the number of items in the list box
-
isEmpty
public boolean isEmpty()Checks if the list box has no items- Returns:
true
if the list box has no items,false
otherwise
-
getItemCount
public int getItemCount()Returns the number of items currently in the list box- Returns:
- Number of items in the list box
-
getItems
Returns a copy of the items in the list box as aList
- Returns:
- Copy of all the items in this list box
-
setSelectedIndex
Sets which item in the list box that is currently selected. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such asCheckBoxList
where individual items have a certain checked/unchecked state. This method will clip the supplied index to within 0 to items.size() -1.- Parameters:
index
- Index of the item that should be currently selected- Returns:
- Itself
-
getSelectedIndex
public int getSelectedIndex()Returns the index of the currently selected item in the list box. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such asCheckBoxList
where individual items have a certain checked/unchecked state.- Returns:
- The index of the currently selected row in the list box, or -1 if there are no items
-
getSelectedItem
Returns the currently selected item in the list box. Please note that in this context, selected simply means it is the item that currently has input focus. This is not to be confused with list box implementations such asCheckBoxList
where individual items have a certain checked/unchecked state.- Returns:
- The currently selected item in the list box, or
null
if there are no items
-