- java.lang.Object
-
- com.googlecode.lanterna.gui2.AbstractComponent<T>
-
- 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
public class Table<V> extends AbstractInteractableComponent<Table<V>>
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 com.googlecode.lanterna.gui2.Interactable
Interactable.FocusChangeDirection, Interactable.Result
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
cellSelection
private boolean
escapeByArrowKey
private java.lang.Runnable
selectAction
private int
selectedColumn
private int
selectedRow
private TableCellRenderer<V>
tableCellRenderer
private TableHeaderRenderer<V>
tableHeaderRenderer
private TableModel<V>
tableModel
private TableModel.Listener<V>
tableModelListener
private int
visibleColumns
private int
visibleRows
-
Constructor Summary
Constructors Constructor Description Table(java.lang.String... columnLabels)
Creates a newTable
with the number of columns as specified by the array of labels
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected TableRenderer<V>
createDefaultRenderer()
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
getColumnByMouseAction(MouseAction mouseAction)
By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
and by comparing widths of column headers, gets column clicked on by mouse action.int
getFirstViewedRowIndex()
Returns the index of the first row that is currently visible.int
getLastViewedRowIndex()
Returns the index of the last row that is currently visible.TableRenderer<V>
getRenderer()
Returns the renderer used to draw this component and measure its preferred size.protected int
getRowByMouseAction(MouseAction mouseAction)
By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets row clicked on by mouse action.int
getSelectedColumn()
Returns the currently selection column index, if in cell-selection mode.int
getSelectedRow()
Returns the index of the currently selected rowTableCellRenderer<V>
getTableCellRenderer()
Returns theTableCellRenderer
used by this table when drawing cellsTableHeaderRenderer<V>
getTableHeaderRenderer()
Returns theTableHeaderRenderer
used by this table when drawing the table's headerTableModel<V>
getTableModel()
Returns the underlying table modelint
getViewLeftColumn()
Deprecated.Use the table renderers method insteadint
getViewTopRow()
Deprecated.Use the table renderers method insteadint
getVisibleColumns()
Returns the number of columns this table will show.int
getVisibleRows()
Returns the number of rows this table will show.Interactable.Result
handleKeyStroke(KeyStroke keyStroke)
This method can be overridden to handle various user input (mostly from the keyboard) when this component is in focus.boolean
isCellSelection()
Returnstrue
if this table is in cell-selection mode, otherwisefalse
boolean
isEscapeByArrowKey()
Returnstrue
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.Table<V>
setCellSelection(boolean cellSelection)
Iftrue
, the user will be able to select and navigate individual cells, otherwise the user can only select full rows.Table<V>
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.Table<V>
setSelectAction(java.lang.Runnable selectAction)
Assigns an action to run whenever the user presses the enter or space key while focused on the table.Table<V>
setSelectedColumn(int selectedColumn)
If in cell selection mode, updates which column is selected and ensures the selected column is visible in the view.Table<V>
setSelectedRow(int selectedRow)
Sets the index of the selected row and ensures the selected row is visible in the viewTable<V>
setTableCellRenderer(TableCellRenderer<V> tableCellRenderer)
Replaces theTableCellRenderer
used by this table when drawing cellsTable<V>
setTableHeaderRenderer(TableHeaderRenderer<V> tableHeaderRenderer)
Replaces theTableHeaderRenderer
used by this table when drawing the table's headerTable<V>
setTableModel(TableModel<V> tableModel)
Updates the table with a new table model, effectively replacing the content of the table completelyTable<V>
setViewLeftColumn(int viewLeftColumn)
Deprecated.Use the table renderers method insteadTable<V>
setViewTopRow(int viewTopRow)
Deprecated.Use the table renderers method insteadvoid
setVisibleColumns(int visibleColumns)
Sets the number of columns this table should show.void
setVisibleRows(int visibleRows)
Sets the number of rows this table will show.-
Methods inherited from class com.googlecode.lanterna.gui2.AbstractInteractableComponent
afterEnterFocus, afterLeaveFocus, getCursorLocation, getInputFilter, 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, 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 Detail
-
tableModel
private TableModel<V> tableModel
-
tableModelListener
private TableModel.Listener<V> tableModelListener
-
tableHeaderRenderer
private TableHeaderRenderer<V> tableHeaderRenderer
-
tableCellRenderer
private TableCellRenderer<V> tableCellRenderer
-
selectAction
private java.lang.Runnable 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
-
-
Method Detail
-
getTableModel
public TableModel<V> getTableModel()
Returns the underlying table model- Returns:
- Underlying table model
-
setTableModel
public Table<V> setTableModel(TableModel<V> tableModel)
Updates the table with a new table model, effectively replacing the content of the table completely- Parameters:
tableModel
- New table model- Returns:
- Itself
-
getTableCellRenderer
public TableCellRenderer<V> getTableCellRenderer()
Returns theTableCellRenderer
used by this table when drawing cells- Returns:
TableCellRenderer
used by this table when drawing cells
-
setTableCellRenderer
public Table<V> setTableCellRenderer(TableCellRenderer<V> tableCellRenderer)
Replaces theTableCellRenderer
used by this table when drawing cells- Parameters:
tableCellRenderer
- NewTableCellRenderer
to use- Returns:
- Itself
-
getTableHeaderRenderer
public TableHeaderRenderer<V> getTableHeaderRenderer()
Returns theTableHeaderRenderer
used by this table when drawing the table's header- Returns:
TableHeaderRenderer
used by this table when drawing the table's header
-
setTableHeaderRenderer
public Table<V> setTableHeaderRenderer(TableHeaderRenderer<V> tableHeaderRenderer)
Replaces theTableHeaderRenderer
used by this table when drawing the table's header- Parameters:
tableHeaderRenderer
- NewTableHeaderRenderer
to 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 public int 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 public Table<V> setViewTopRow(int viewTopRow)
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 public int 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 public Table<V> setViewLeftColumn(int viewLeftColumn)
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
public Table<V> setSelectedColumn(int selectedColumn)
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
public Table<V> setSelectedRow(int selectedRow)
Sets the index of the selected row and ensures the selected row is visible in the view- Parameters:
selectedRow
- Index of the row to select- Returns:
- Itself
-
setCellSelection
public Table<V> setCellSelection(boolean cellSelection)
Iftrue
, the user will be able to select and navigate individual cells, otherwise the user can only select full rows.- Parameters:
cellSelection
-true
if cell selection should be enabled,false
for row selection- Returns:
- Itself
-
isCellSelection
public boolean isCellSelection()
Returnstrue
if this table is in cell-selection mode, otherwisefalse
- Returns:
true
if this table is in cell-selection mode, otherwisefalse
-
setSelectAction
public Table<V> setSelectAction(java.lang.Runnable selectAction)
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()
Returnstrue
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. WithescapeByArrowKey
set totrue
, this will move focus away from the table in the direction the user pressed, iffalse
then nothing will happen.- Returns:
true
if user can switch focus away from the table using arrow keys,false
otherwise
-
setEscapeByArrowKey
public Table<V> 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. WithescapeByArrowKey
set totrue
, this will move focus away from the table in the direction the user pressed, iffalse
then nothing will happen.- Parameters:
escapeByArrowKey
-true
if user can switch focus away from the table using arrow keys,false
otherwise- Returns:
- Itself
-
createDefaultRenderer
protected TableRenderer<V> 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<Table<V>>
- Returns:
- Renderer to use when sizing and drawing this component
-
getRenderer
public TableRenderer<V> getRenderer()
Description copied from interface:Component
Returns 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:
getRenderer
in interfaceComponent
- Overrides:
getRenderer
in classAbstractInteractableComponent<Table<V>>
- Returns:
- Renderer this component is using
-
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 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<Table<V>>
- Parameters:
keyStroke
- What input was entered by the user- Returns:
- Result of processing the key-stroke
-
getRowByMouseAction
protected int getRowByMouseAction(MouseAction mouseAction)
By convertingTerminalPosition
s toAbstractComponent.toGlobal(TerminalPosition)
gets row clicked on by mouse action.- Returns:
- row of a table that was clicked on with
MouseAction
-
getColumnByMouseAction
protected int getColumnByMouseAction(MouseAction mouseAction)
By convertingTerminalPosition
s 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
-
-