Class TableModel<V>

java.lang.Object
com.googlecode.lanterna.gui2.table.TableModel<V>

public class TableModel<V> extends Object
A TableModel contains the data model behind a table, here is where all the action cell values and header labels are stored.
  • Field Details

  • Constructor Details

    • TableModel

      public TableModel(String... columnLabels)
      Default constructor, creates a new model with same number of columns as labels supplied
      Parameters:
      columnLabels - Labels for the column headers
  • Method Details

    • getColumnCount

      public int getColumnCount()
      Returns the number of columns in the model
      Returns:
      Number of columns in the model
    • getRowCount

      public int getRowCount()
      Returns number of rows in the model
      Returns:
      Number of rows in the model
    • getRows

      public List<List<V>> getRows()
      Returns all rows in the model as a list of lists containing the data as elements
      Returns:
      All rows in the model as a list of lists containing the data as elements
    • getColumnLabels

      public List<String> getColumnLabels()
      Returns all column header label as a list of strings
      Returns:
      All column header label as a list of strings
    • getRow

      public List<V> getRow(int index)
      Returns a row from the table as a list of the cell data
      Parameters:
      index - Index of the row to return
      Returns:
      Row from the table as a list of the cell data
    • addRow

      @SafeVarargs public final TableModel<V> addRow(V... values)
      Adds a new row to the table model at the end. This may update the selection to make sure the same row is selected.
      Parameters:
      values - Data to associate with the new row, mapped column by column in order
      Returns:
      Itself
    • addRow

      public TableModel<V> addRow(Collection<V> values)
      Adds a new row to the table model at the end. This may update the selection to make sure the same row is selected.
      Parameters:
      values - Data to associate with the new row, mapped column by column in order
      Returns:
      Itself
    • insertRow

      public TableModel<V> insertRow(int index, Collection<V> values)
      Inserts a new row to the table model at a particular index. This may update the selection to make sure the same row is selected.
      Parameters:
      index - Index the new row should have, 0 means the first row and row count will append the row at the end
      values - Data to associate with the new row, mapped column by column in order
      Returns:
      Itself
    • removeRow

      public TableModel<V> removeRow(int index)
      Removes a row at a particular index from the table model. This may update the selection to make sure the same row is selected.
      Parameters:
      index - Index of the row to remove
      Returns:
      Itself
    • clear

      public TableModel<V> clear()
      Removes all rows from the table, this will trigger listeners for each row
      Returns:
      Itself
    • getColumnLabel

      public String getColumnLabel(int index)
      Returns the label of a column header
      Parameters:
      index - Index of the column to retrieve the header label for
      Returns:
      Label of the column selected
    • setColumnLabel

      public TableModel<V> setColumnLabel(int index, String newLabel)
      Updates the label of a column header
      Parameters:
      index - Index of the column to update the header label for
      newLabel - New label to assign to the column header
      Returns:
      Itself
    • addColumn

      public TableModel<V> addColumn(String label, V[] newColumnValues)
      Adds a new column into the table model as the last column. You can optionally supply values for the existing rows through the newColumnValues.
      Parameters:
      label - Label for the header of the new column
      newColumnValues - Optional values to assign to the existing rows, where the first element in the array will be the value of the first row and so on...
      Returns:
      Itself
    • insertColumn

      public TableModel<V> insertColumn(int index, String label, V[] newColumnValues)
      Adds a new column into the table model at a specified index. You can optionally supply values for the existing rows through the newColumnValues.
      Parameters:
      index - Index for the new column
      label - Label for the header of the new column
      newColumnValues - Optional values to assign to the existing rows, where the first element in the array will be the value of the first row and so on...
      Returns:
      Itself
    • removeColumn

      public TableModel<V> removeColumn(int index)
      Removes a column from the table model
      Parameters:
      index - Index of the column to remove
      Returns:
      Itself
    • getCell

      public V getCell(int columnIndex, int rowIndex)
      Returns the cell value stored at a specific column/row coordinate.
      Parameters:
      columnIndex - Column index of the cell
      rowIndex - Row index of the cell
      Returns:
      The data value stored in this cell
    • setCell

      public TableModel<V> setCell(int columnIndex, int rowIndex, V value)
      Updates the call value stored at a specific column/row coordinate.
      Parameters:
      columnIndex - Column index of the cell
      rowIndex - Row index of the cell
      value - New value to assign to the cell
      Returns:
      Itself
    • addListener

      public TableModel<V> addListener(TableModel.Listener<V> listener)
      Adds a listener to this table model that will be notified whenever the model changes
      Parameters:
      listener - TableModel.Listener to register with this model
      Returns:
      Itself
    • removeListener

      public TableModel<V> removeListener(TableModel.Listener<V> listener)
      Removes a listener from this model so that it will no longer receive any notifications when the model changes
      Parameters:
      listener - TableModel.Listener to deregister from this model
      Returns:
      Itself