Type Parameters:
V - Type of data to store in the table cells, presented through toString()
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.
  • Field Details

    • tableModel

      private TableModel<V> tableModel
    • tableModelListener

      private TableModel.Listener<V> tableModelListener
    • tableHeaderRenderer

      private TableHeaderRenderer<V> tableHeaderRenderer
    • tableCellRenderer

      private TableCellRenderer<V> tableCellRenderer
    • selectAction

      private 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
  • Constructor Details

    • Table

      public Table(String... columnLabels)
      Creates a new Table with 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

      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 the TableCellRenderer 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 the TableCellRenderer used by this table when drawing cells
      Parameters:
      tableCellRenderer - New TableCellRenderer to use
      Returns:
      Itself
    • getTableHeaderRenderer

      public TableHeaderRenderer<V> getTableHeaderRenderer()
      Returns the TableHeaderRenderer 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 the TableHeaderRenderer used by this table when drawing the table's header
      Parameters:
      tableHeaderRenderer - New TableHeaderRenderer 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 instead
      Returns 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 (through setViewTopRow(..)) 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 instead
      Sets 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 instead
      Returns 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 (through setViewLeftColumn(..)) 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 instead
      Sets 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)
      If true, 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()
      Returns true if this table is in cell-selection mode, otherwise false
      Returns:
      true if this table is in cell-selection mode, otherwise false
    • setSelectAction

      public Table<V> setSelectAction(Runnable selectAction)
      Assigns an action to run whenever the user presses the enter or space key while focused on the table. If called with null, no action will be run.
      Parameters:
      selectAction - Action to perform when user presses the enter or space key
      Returns:
      Itself
    • isEscapeByArrowKey

      public boolean isEscapeByArrowKey()
      Returns true 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. With escapeByArrowKey set to true, this will move focus away from the table in the direction the user pressed, if false 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. With escapeByArrowKey set to true, this will move focus away from the table in the direction the user pressed, if false 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 class AbstractInteractableComponent<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 interface Component
      Overrides:
      getRenderer in class AbstractInteractableComponent<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 in AbstractInteractableComponent 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 class AbstractInteractableComponent<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 converting TerminalPositions to AbstractComponent.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 converting TerminalPositions to AbstractComponent.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