Class TableModel<V>


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

      Nested Classes 
      Modifier and Type Class Description
      static interface  TableModel.Listener<V>
      Listener interface for the TableModel class which can be attached to a TableModel to be notified of changes to the table model.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.List<java.lang.String> columns  
      private java.util.List<TableModel.Listener<V>> listeners  
      private java.util.List<java.util.List<V>> rows  
    • Constructor Summary

      Constructors 
      Constructor Description
      TableModel​(java.lang.String... columnLabels)
      Default constructor, creates a new model with same number of columns as labels supplied
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      TableModel<V> addColumn​(java.lang.String label, V[] newColumnValues)
      Adds a new column into the table model as the last column.
      TableModel<V> addListener​(TableModel.Listener<V> listener)
      Adds a listener to this table model that will be notified whenever the model changes
      TableModel<V> addRow​(java.util.Collection<V> values)
      Adds a new row to the table model at the end.
      TableModel<V> addRow​(V... values)
      Adds a new row to the table model at the end.
      TableModel<V> clear()
      Removes all rows from the table, this will trigger listeners for each row
      V getCell​(int columnIndex, int rowIndex)
      Returns the cell value stored at a specific column/row coordinate.
      int getColumnCount()
      Returns the number of columns in the model
      java.lang.String getColumnLabel​(int index)
      Returns the label of a column header
      java.util.List<java.lang.String> getColumnLabels()
      Returns all column header label as a list of strings
      java.util.List<V> getRow​(int index)
      Returns a row from the table as a list of the cell data
      int getRowCount()
      Returns number of rows in the model
      java.util.List<java.util.List<V>> getRows()
      Returns all rows in the model as a list of lists containing the data as elements
      TableModel<V> insertColumn​(int index, java.lang.String label, V[] newColumnValues)
      Adds a new column into the table model at a specified index.
      TableModel<V> insertRow​(int index, java.util.Collection<V> values)
      Inserts a new row to the table model at a particular index.
      TableModel<V> removeColumn​(int index)
      Removes a column from the table model
      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
      TableModel<V> removeRow​(int index)
      Removes a row at a particular index from the table model.
      TableModel<V> setCell​(int columnIndex, int rowIndex, V value)
      Updates the call value stored at a specific column/row coordinate.
      TableModel<V> setColumnLabel​(int index, java.lang.String newLabel)
      Updates the label of a column header
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • columns

        private final java.util.List<java.lang.String> columns
      • rows

        private final java.util.List<java.util.List<V>> rows
    • Constructor Detail

      • TableModel

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

      • 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 java.util.List<java.util.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 java.util.List<java.lang.String> getColumnLabels()
        Returns all column header label as a list of strings
        Returns:
        All column header label as a list of strings
      • getRow

        public java.util.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​(java.util.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,
                                       java.util.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 java.lang.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,
                                            java.lang.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​(java.lang.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,
                                          java.lang.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