Class Table<V>
java.lang.Object
com.googlecode.lanterna.gui2.AbstractComponent<Table<V>>
com.googlecode.lanterna.gui2.AbstractInteractableComponent<Table<V>>
com.googlecode.lanterna.gui2.table.Table<V>
- Type Parameters:
V- Type of data to store in the table cells, presented throughtoString()
- All Implemented Interfaces:
Component, Interactable, TextGUIElement
The table class is an interactable component that displays a grid of cells containing data along with a header of
labels. It supports scrolling when the number of rows and/or columns gets too large to fit and also supports
user selection which is either row-based or cell-based. User will move the current selection by using the arrow keys
on the keyboard.
-
Nested Class Summary
Nested classes/interfaces inherited from interface Interactable
Interactable.FocusChangeDirection, Interactable.Result -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate booleanprivate Runnableprivate intprivate intprivate TableCellRenderer<V> private TableHeaderRenderer<V> private TableModel<V> private TableModel.Listener<V> private intprivate int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected TableRenderer<V> 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 intgetColumnByMouseAction(MouseAction mouseAction) By convertingTerminalPositions toAbstractComponent.toGlobal(TerminalPosition)and by comparing widths of column headers, gets column clicked on by mouse action.intReturns the index of the first row that is currently visible.intReturns the index of the last row that is currently visible.Returns the renderer used to draw this component and measure its preferred size.protected intgetRowByMouseAction(MouseAction mouseAction) By convertingTerminalPositions toAbstractComponent.toGlobal(TerminalPosition)gets row clicked on by mouse action.intReturns the currently selection column index, if in cell-selection mode.intReturns the index of the currently selected rowReturns theTableCellRendererused by this table when drawing cellsReturns theTableHeaderRendererused by this table when drawing the table's headerReturns the underlying table modelintDeprecated.Use the table renderers method insteadintDeprecated.Use the table renderers method insteadintReturns the number of columns this table will show.intReturns the number of rows this table will show.handleKeyStroke(KeyStroke keyStroke) This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.booleanReturnstrueif this table is in cell-selection mode, otherwisefalsebooleanReturnstrueif this table can be navigated away from when the selected row is at one of the extremes and the user presses the array key to continue in that direction.setCellSelection(boolean cellSelection) Iftrue, the user will be able to select and navigate individual cells, otherwise the user can only select full rows.setEscapeByArrowKey(boolean escapeByArrowKey) Sets the flag for if this table can be navigated away from when the selected row is at one of the extremes and the user presses the array key to continue in that direction.setSelectAction(Runnable selectAction) Assigns an action to run whenever the user presses the enter or space key while focused on the table.setSelectedColumn(int selectedColumn) If in cell selection mode, updates which column is selected and ensures the selected column is visible in the view.setSelectedRow(int selectedRow) Sets the index of the selected row and ensures the selected row is visible in the viewsetTableCellRenderer(TableCellRenderer<V> tableCellRenderer) Replaces theTableCellRendererused by this table when drawing cellssetTableHeaderRenderer(TableHeaderRenderer<V> tableHeaderRenderer) Replaces theTableHeaderRendererused by this table when drawing the table's headersetTableModel(TableModel<V> tableModel) Updates the table with a new table model, effectively replacing the content of the table completelysetViewLeftColumn(int viewLeftColumn) Deprecated.Use the table renderers method insteadsetViewTopRow(int viewTopRow) Deprecated.Use the table renderers method insteadvoidsetVisibleColumns(int visibleColumns) Sets the number of columns this table should show.voidsetVisibleRows(int visibleRows) Sets the number of rows this table will show.Methods inherited from class AbstractInteractableComponent
afterEnterFocus, afterLeaveFocus, getCursorLocation, getInputFilter, handleInput, isActivationStroke, isEnabled, isFocusable, isFocused, isKeyboardActivationStroke, isMouseActivationStroke, isMouseDown, isMouseDrag, isMouseMove, isMouseUp, onEnterFocus, onLeaveFocus, setEnabled, setInputFilter, takeFocusMethods inherited from class 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, withBorderMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface 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, withBorderMethods inherited from interface TextGUIElement
draw, isInvalid
-
Field Details
-
tableModel
-
tableModelListener
-
tableHeaderRenderer
-
tableCellRenderer
-
selectAction
-
cellSelection
private boolean cellSelection -
visibleRows
private int visibleRows -
visibleColumns
private int visibleColumns -
selectedRow
private int selectedRow -
selectedColumn
private int selectedColumn -
escapeByArrowKey
private boolean escapeByArrowKey
-
-
Constructor Details
-
Table
Creates a newTablewith the number of columns as specified by the array of labels- Parameters:
columnLabels- Creates one column per label in the array, must be more than one
-
-
Method Details
-
getTableModel
Returns the underlying table model- Returns:
- Underlying table model
-
setTableModel
Updates the table with a new table model, effectively replacing the content of the table completely- Parameters:
tableModel- New table model- Returns:
- Itself
-
getTableCellRenderer
Returns theTableCellRendererused by this table when drawing cells- Returns:
TableCellRendererused by this table when drawing cells
-
setTableCellRenderer
Replaces theTableCellRendererused by this table when drawing cells- Parameters:
tableCellRenderer- NewTableCellRendererto use- Returns:
- Itself
-
getTableHeaderRenderer
Returns theTableHeaderRendererused by this table when drawing the table's header- Returns:
TableHeaderRendererused by this table when drawing the table's header
-
setTableHeaderRenderer
Replaces theTableHeaderRendererused by this table when drawing the table's header- Parameters:
tableHeaderRenderer- NewTableHeaderRendererto use- Returns:
- Itself
-
setVisibleColumns
public void setVisibleColumns(int visibleColumns) Sets the number of columns this table should show. If there are more columns in the table model, a scrollbar will be used to allow the user to scroll left and right and view all columns.- Parameters:
visibleColumns- Number of columns to display at once
-
getVisibleColumns
public int getVisibleColumns()Returns the number of columns this table will show. If there are more columns in the table model, a scrollbar will be used to allow the user to scroll left and right and view all columns.- Returns:
- Number of visible columns for this table
-
setVisibleRows
public void setVisibleRows(int visibleRows) Sets the number of rows this table will show. If there are more rows in the table model, a scrollbar will be used to allow the user to scroll up and down and view all rows.- Parameters:
visibleRows- Number of rows to display at once
-
getVisibleRows
public int getVisibleRows()Returns the number of rows this table will show. If there are more rows in the table model, a scrollbar will be used to allow the user to scroll up and down and view all rows.- Returns:
- Number of rows to display at once
-
getViewTopRow
Deprecated.Use the table renderers method insteadReturns the index of the row that is currently the first row visible. This is always 0 unless scrolling has been enabled and either the user or the software (throughsetViewTopRow(..)) has scrolled down.- Returns:
- Index of the row that is currently the first row visible
-
getFirstViewedRowIndex
public int getFirstViewedRowIndex()Returns the index of the first row that is currently visible.- Returns:
- the index of the first row that is currently visible
-
getLastViewedRowIndex
public int getLastViewedRowIndex()Returns the index of the last row that is currently visible.- Returns:
- the index of the last row that is currently visible
-
setViewTopRow
Deprecated.Use the table renderers method insteadSets the view row offset for the first row to display in the table. Calling this with 0 will make the first row in the model be the first visible row in the table.- Parameters:
viewTopRow- Index of the row that is currently the first row visible- Returns:
- Itself
-
getViewLeftColumn
Deprecated.Use the table renderers method insteadReturns the index of the column that is currently the first column visible. This is always 0 unless scrolling has been enabled and either the user or the software (throughsetViewLeftColumn(..)) has scrolled to the right.- Returns:
- Index of the column that is currently the first column visible
-
setViewLeftColumn
Deprecated.Use the table renderers method insteadSets the view column offset for the first column to display in the table. Calling this with 0 will make the first column in the model be the first visible column in the table.- Parameters:
viewLeftColumn- Index of the column that is currently the first column visible- Returns:
- Itself
-
getSelectedColumn
public int getSelectedColumn()Returns the currently selection column index, if in cell-selection mode. Otherwise it returns -1.- Returns:
- In cell-selection mode returns the index of the selected column, otherwise -1
-
setSelectedColumn
If in cell selection mode, updates which column is selected and ensures the selected column is visible in the view. If not in cell selection mode, does nothing.- Parameters:
selectedColumn- Index of the column that should be selected- Returns:
- Itself
-
getSelectedRow
public int getSelectedRow()Returns the index of the currently selected row- Returns:
- Index of the currently selected row
-
setSelectedRow
-
setCellSelection
-
isCellSelection
public boolean isCellSelection()Returnstrueif this table is in cell-selection mode, otherwisefalse- Returns:
trueif this table is in cell-selection mode, otherwisefalse
-
setSelectAction
Assigns an action to run whenever the user presses the enter or space key while focused on the table. If called withnull, no action will be run.- Parameters:
selectAction- Action to perform when user presses the enter or space key- Returns:
- Itself
-
isEscapeByArrowKey
public boolean isEscapeByArrowKey()Returnstrueif this table can be navigated away from when the selected row is at one of the extremes and the user presses the array key to continue in that direction. WithescapeByArrowKeyset totrue, this will move focus away from the table in the direction the user pressed, iffalsethen nothing will happen.- Returns:
trueif user can switch focus away from the table using arrow keys,falseotherwise
-
setEscapeByArrowKey
Sets the flag for if this table can be navigated away from when the selected row is at one of the extremes and the user presses the array key to continue in that direction. WithescapeByArrowKeyset totrue, this will move focus away from the table in the direction the user pressed, iffalsethen nothing will happen.- Parameters:
escapeByArrowKey-trueif user can switch focus away from the table using arrow keys,falseotherwise- Returns:
- Itself
-
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<Table<V>>- Returns:
- Renderer to use when sizing and drawing this component
-
getRenderer
Description copied from interface:ComponentReturns the renderer used to draw this component and measure its preferred size. You probably won't need to call this method unless you know exactly which ComponentRenderer implementation is used and you need to customize it.- Specified by:
getRendererin interfaceComponent- Overrides:
getRendererin classAbstractInteractableComponent<Table<V>>- Returns:
- Renderer this component is using
-
handleKeyStroke
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<Table<V>>- Parameters:
keyStroke- What input was entered by the user- Returns:
- Result of processing the key-stroke
-
getRowByMouseAction
By convertingTerminalPositions toAbstractComponent.toGlobal(TerminalPosition)gets row clicked on by mouse action.- Returns:
- row of a table that was clicked on with
MouseAction
-
getColumnByMouseAction
By convertingTerminalPositions toAbstractComponent.toGlobal(TerminalPosition)and by comparing widths of column headers, gets column clicked on by mouse action.- Returns:
- row of a table that was clicked on with
MouseAction
-