Holger's Java API

## com.antelmann.util.table
Class SortableTableModel

    java.lang.Object
      javax.swing.table.AbstractTableModel
          com.antelmann.util.table.SortableTableModel

All Implemented Interfaces:
  Wrapped<TableModel>, Serializable, EventListener, TableModelListener, TableModel

    public class SortableTableModel

extends AbstractTableModel
implements TableModelListener, Wrapped<TableModel>

a wrapper around a TableModel allowing to sort the model.

Since:
  6/2/06
Author:
  Holger Antelmann
See Also:
  Serialized Form

Field Summary
`static int` `NO_COLUMN`

Fields inherited from class javax.swing.table.AbstractTableModel
`listenerList`

Constructor Summary
`SortableTableModel(TableModel model, Comparator<?>... comp)` constructs a SortableTableModel with an embedded model and Comparator objects for each column.
`SortableTableModel(TableModel model, List<Comparator<?>> comp)` constructs a SortableTableModel with an embedded model and Comparator objects for each column.

Method Summary
`protected void` `finalize()` allows itself to be garbage collected by removing itself as a listener to the embedded model
`Class<?>` `getColumnClass(int column)`
`int` `getColumnCount()`
`String` `getColumnName(int column)`
`Comparator<?>` `getComparatorForColumn(int column)` return value may be null, in wich case the default would be used for sorting
`Comparator<Object>` `getDefaultComparator()` returns the Comparator in use if the column comparator is null; returns an instance of a UniversalComparator by default
`int` `getOriginalRow(int sortedRowPosition)`
`int` `getRowCount()`
`int` `getRowPosition(int originalRow)`
`List<Integer>` `getSortingView()` returns an unmodifiable view of the current sorting
`Object` `getValueAt(int row, int column)`
`boolean` `isCellEditable(int row, int column)`
`int` `lastRowSorted()`
`boolean` `lastSortAscending()`
`Comparator<?>` `setComparatorForColumn(int column, Comparator<?> comp)` sets the Comparator for the specified column (may be null, in which case the default comparator is used)
`void` `setDefaultComparator(Comparator<Object> defaultComparator)` allows to specify a general Comparator for columns that have no specific Comparator set
`void` `setValueAt(Object value, int row, int column)`
`void` `sortByColumn(int column)` calls sortByColumn(column, true)
`void` `sortByColumn(int column, boolean ascending)` if column is `NO_COLUMN`, the table will be reset to be in an unsorted state and the second parameter is ignored
`void` `tableChanged(TableModelEvent ev)` public as an implementation side effect; resets the table to its updated model (table is sorted as before herafter) unless it's only a single row that has been updated.
`void` `toggleSorting(int column)` either sorts by the given solumn or - if that column is already sorted - reverses the current column sorting (from ascending to descending or vice versa).
`TableModel` `unwrap()` returns the embedded instance that this wrapper encloses

Methods inherited from class javax.swing.table.AbstractTableModel
`addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener`

Methods inherited from class java.lang.Object
`clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### NO_COLUMN

    public static final int NO_COLUMN

See Also:
  Constant Field Values

Constructor Detail

### SortableTableModel

    public SortableTableModel(TableModel model,
                              Comparator<?>... comp)

  constructs a SortableTableModel with an embedded model and Comparator objects for each column. If there are less comparators than columns (or an entry in the array is null), the sorting is either done by the column's natural sorting or (if the column class doesn't implement Comparable) a default Comparator is used.

  See Also:
    `UniversalComparator`

### SortableTableModel

    public SortableTableModel(TableModel model,
                              List<Comparator<?>> comp)

  constructs a SortableTableModel with an embedded model and Comparator objects for each column. If there are less comparators than columns (or an entry in the list is null), the sorting is either done by the column's natural sorting or (if the column class doesn't implement Comparable) a default Comparator is used.

  See Also:
    `UniversalComparator`

Method Detail

### finalize

    protected void finalize()

  allows itself to be garbage collected by removing itself as a listener to the embedded model

  Overrides:
    `finalize` in class `Object`

### getComparatorForColumn

    public Comparator<?> getComparatorForColumn(int column)

  return value may be null, in wich case the default would be used for sorting

### getDefaultComparator

    public Comparator<Object> getDefaultComparator()

  returns the Comparator in use if the column comparator is null; returns an instance of a UniversalComparator by default

  See Also:
    `UniversalComparator`

### setDefaultComparator

    public void setDefaultComparator(Comparator<Object> defaultComparator)

  allows to specify a general Comparator for columns that have no specific Comparator set

### setComparatorForColumn

    public Comparator<?> setComparatorForColumn(int column,
                                                Comparator<?> comp)

  sets the Comparator for the specified column (may be null, in which case the default comparator is used). ( @see com.antelmann.util.UniversalComparator

  Returns:
    the Comparator previously set

### tableChanged

    public void tableChanged(TableModelEvent ev)

  public as an implementation side effect; resets the table to its updated model (table is sorted as before herafter) unless it's only a single row that has been updated. Note that - in case of structural changes - the Comparators set may not be adequate anymore, or the sorting could result in an error as the sorting column may not exist anymore.

  Specified by:
    `tableChanged` in interface `TableModelListener`

### toggleSorting

    public void toggleSorting(int column)

  either sorts by the given solumn or - if that column is already sorted - reverses the current column sorting (from ascending to descending or vice versa).

### sortByColumn

    public void sortByColumn(int column)

  calls sortByColumn(column, true)

### sortByColumn

    public void sortByColumn(int column,
                             boolean ascending)
                      throws IllegalArgumentException

  if column is `NO_COLUMN`, the table will be reset to be in an unsorted state and the second parameter is ignored

  Throws:
    `IllegalArgumentException`

### unwrap

    public TableModel unwrap()

  Description copied from interface: `Wrapped`
  returns the embedded instance that this wrapper encloses

  Specified by:
    `unwrap` in interface `Wrapped<TableModel>`

### getOriginalRow

    public int getOriginalRow(int sortedRowPosition)

### getRowPosition

    public int getRowPosition(int originalRow)

### getSortingView

    public List<Integer> getSortingView()

  returns an unmodifiable view of the current sorting

### lastRowSorted

    public int lastRowSorted()

### lastSortAscending

    public boolean lastSortAscending()

### getRowCount

    public int getRowCount()

  Specified by:
    `getRowCount` in interface `TableModel`

### getColumnCount

    public int getColumnCount()

  Specified by:
    `getColumnCount` in interface `TableModel`

### getColumnName

    public String getColumnName(int column)

  Specified by:
    `getColumnName` in interface `TableModel`
  Overrides:
    `getColumnName` in class `AbstractTableModel`

### getColumnClass

    public Class<?> getColumnClass(int column)

  Specified by:
    `getColumnClass` in interface `TableModel`
  Overrides:
    `getColumnClass` in class `AbstractTableModel`

### isCellEditable

    public boolean isCellEditable(int row,
                                  int column)

  Specified by:
    `isCellEditable` in interface `TableModel`
  Overrides:
    `isCellEditable` in class `AbstractTableModel`

### getValueAt

    public Object getValueAt(int row,
                             int column)

  Specified by:
    `getValueAt` in interface `TableModel`

### setValueAt

    public void setValueAt(Object value,
                           int row,
                           int column)

  Specified by:
    `setValueAt` in interface `TableModel`
  Overrides:
    `setValueAt` in class `AbstractTableModel`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads