APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.table

public Class TableRowSorter<M extends TableModel>

extends DefaultRowSorter<M extends TableModel, Integer>
Class Inheritance
Known Direct Subclasses
sun.swing.FilePane.DetailsTableRowSorter
Type Parameters
<M>
the type of the model, which must be an implementation of TableModel
Imports
java.text.Collator, java.util.*, javax.swing.DefaultRowSorter, .RowFilter, .SortOrder

An implementation of RowSorter that provides sorting and filtering using a TableModel. The following example shows adding sorting to a JTable:
  TableModel myModel = createMyTableModel();
  JTable table = new JTable(myModel);
  table.setRowSorter(new TableRowSorter(myModel));
This will do all the wiring such that when the user does the appropriate gesture, such as clicking on the column header, the table will visually sort.

JTable's row-based methods and JTable's selection model refer to the view and not the underlying model. Therefore, it is necessary to convert between the two. For example, to get the selection in terms of myModel you need to convert the indices:

  int[] selection = table.getSelectedRows();
  for (int i = 0; i < selection.length; i++) {
    selection[i] = table.convertRowIndexToModel(selection[i]);
  }
Similarly to select a row in JTable based on a coordinate from the underlying model do the inverse:
  table.setRowSelectionInterval(table.convertRowIndexToView(row),
                                table.convertRowIndexToView(row));

The previous example assumes you have not enabled filtering. If you have enabled filtering convertRowIndexToView will return -1 for locations that are not visible in the view.

TableRowSorter uses Comparators for doing comparisons. The following defines how a Comparator is chosen for a column:

  1. If a Comparator has been specified for the column by the setComparator method, use it.
  2. If the column class as returned by getColumnClass is String, use the Comparator returned by Collator.getInstance().
  3. If the column class implements Comparable, use a Comparator that invokes the compareTo method.
  4. If a TableStringConverter has been specified, use it to convert the values to Strings and then use the Comparator returned by Collator.getInstance().
  5. Otherwise use the Comparator returned by Collator.getInstance() on the results from calling toString on the objects.

In addition to sorting TableRowSorter provides the ability to filter. A filter is specified using the setFilter method. The following example will only show rows containing the string "foo":

  TableModel myModel = createMyTableModel();
  TableRowSorter sorter = new TableRowSorter(myModel);
  sorter.setRowFilter(RowFilter.regexFilter(".*foo.*"));
  JTable table = new JTable(myModel);
  table.setRowSorter(sorter);

If the underlying model structure changes (the modelStructureChanged method is invoked) the following are reset to their default values: Comparators by column, current sort order, and whether each column is sortable. The default sort order is natural (the same as the model), and columns are sortable by default.

TableRowSorter has one formal type parameter: the type of the model. Passing in a type that corresponds exactly to your model allows you to filter based on your model without casting. Refer to the documentation of RowFilter for an example of this.

Warning

DefaultTableModel returns a column class of Object. As such all comparisons will be done using toString. This may be unnecessarily expensive. If the column only contains one type of value, such as an Integer, you should override getColumnClass and return the appropriate Class. This will dramatically increase the performance of this class.

Since
1.6
See Also
javax.swing.JTable, javax.swing.RowFilter, javax.swing.table.DefaultTableModel, java.text.Collator, java.util.Comparator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private class
TableRowSorter.TableRowSorterModelWrapper

Implementation of DefaultRowSorter.ModelWrapper that delegates to a TableModel.

Field Summary

Modifier and TypeField and Description
private static final Comparator<?>
COMPARABLE_COMPARATOR

Comparator that uses compareTo on the contents.

private TableStringConverter
stringConverter

For toString conversions.

private M
tableModel

Underlying model.

Constructor Summary

AccessConstructor and Description
public
TableRowSorter()

Creates a TableRowSorter with an empty model.

public
TableRowSorter(M
the underlying TableModel to use, null is treated as an empty model
model
)

Creates a TableRowSorter using model as the underlying TableModel.

Method Summary

Modifier and TypeMethod and Description
public Comparator<?>
getComparator(int column)

Overrides javax.swing.DefaultRowSorter.getComparator.

Returns the Comparator for the specified column.

public TableStringConverter

Returns:

object responsible for converting values to strings.
getStringConverter
()

Returns the object responsible for converting values from the model to strings.

public void
setModel(M
the underlying model to use, or null
model
)

Sets the TableModel to use as the underlying model for this TableRowSorter.

public void
setStringConverter(TableStringConverter
the object responsible for converting values from the model to strings
stringConverter
)

Sets the object responsible for converting values from the model to strings.

protected boolean
useToString(int column)

Overrides javax.swing.DefaultRowSorter.useToString.

Returns whether or not to convert the value to a string before doing comparisons when sorting.

Inherited from javax.swing.DefaultRowSorter:
allRowsChangedconvertRowIndexToModelconvertRowIndexToViewgetMaxSortKeysgetModelgetModelRowCountgetModelWrappergetRowFiltergetSortKeysgetSortsOnUpdatesgetViewRowCountisSortablemodelStructureChangedrowsDeletedrowsInsertedrowsUpdatedrowsUpdatedsetComparatorsetMaxSortKeyssetModelWrappersetRowFiltersetSortablesetSortKeyssetSortsOnUpdatessorttoggleSortOrder

Field Detail

COMPARABLE_COMPARATORback to summary
private static final Comparator<?> COMPARABLE_COMPARATOR

Comparator that uses compareTo on the contents.

stringConverterback to summary
private TableStringConverter stringConverter

For toString conversions.

tableModelback to summary
private M tableModel

Underlying model.

Constructor Detail

TableRowSorterback to summary
public TableRowSorter()

Creates a TableRowSorter with an empty model.

TableRowSorterback to summary
public TableRowSorter(M model)

Creates a TableRowSorter using model as the underlying TableModel.

Parameters
model:M

the underlying TableModel to use, null is treated as an empty model

Method Detail

getComparatorback to summary
public Comparator<?> getComparator(int column)

Overrides javax.swing.DefaultRowSorter.getComparator.

Returns the Comparator for the specified column. If a Comparator has not been specified using the setComparator method a Comparator will be returned based on the column class (TableModel.getColumnClass) of the specified column. If the column class is String, Collator.getInstance is returned. If the column class implements Comparable a private Comparator is returned that invokes the compareTo method. Otherwise Collator.getInstance is returned.

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the underlying model
getStringConverterback to summary
public TableStringConverter getStringConverter()

Returns the object responsible for converting values from the model to strings.

Returns:TableStringConverter

object responsible for converting values to strings.

setModelback to summary
public void setModel(M model)

Sets the TableModel to use as the underlying model for this TableRowSorter. A value of null can be used to set an empty model.

Parameters
model:M

the underlying model to use, or null

setStringConverterback to summary
public void setStringConverter(TableStringConverter stringConverter)

Sets the object responsible for converting values from the model to strings. If non-null this is used to convert any object values, that do not have a registered Comparator, to strings.

Parameters
stringConverter:TableStringConverter

the object responsible for converting values from the model to strings

useToStringback to summary
protected boolean useToString(int column)

Overrides javax.swing.DefaultRowSorter.useToString.

Doc from javax.swing.DefaultRowSorter.useToString.

Returns whether or not to convert the value to a string before doing comparisons when sorting. If true ModelWrapper.getStringValueAt will be used, otherwise ModelWrapper.getValueAt will be used. It is up to subclasses, such as TableRowSorter, to honor this value in their ModelWrapper implementation.

Exceptions
IndexOutOfBoundsException:
if column is not valid
javax.swing.table back to summary

private Class TableRowSorter.ComparableComparator

extends Object
implements Comparator<Object>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(Object o1, Object o2)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ComparableComparatorback to summary
private ComparableComparator()

Method Detail

compareback to summary
public int compare(Object o1, Object o2)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Annotations
@SuppressWarnings:unchecked
javax.swing.table back to summary

private Class TableRowSorter.TableRowSorterModelWrapper

extends DefaultRowSorter.ModelWrapper<M extends TableModel, Integer>
Class Inheritance

Implementation of DefaultRowSorter.ModelWrapper that delegates to a TableModel.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
getColumnCount()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getColumnCount.

Returns the number of columns in the model.

public Integer
getIdentifier(int index)

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getIdentifier.

Returns the identifier for the specified row.

public M
getModel()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getModel.

Returns the underlying model that this Model is wrapping.

public int
getRowCount()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getRowCount.

Returns the number of rows in the model.

public String
getStringValueAt(int row, int column)

Overrides javax.swing.DefaultRowSorter.ModelWrapper.getStringValueAt.

Returns the value as a String at the specified index.

public Object
getValueAt(int row, int column)

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getValueAt.

Returns the value at the specified index.

Constructor Detail

TableRowSorterModelWrapperback to summary
private TableRowSorterModelWrapper()

Method Detail

getColumnCountback to summary
public int getColumnCount()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getColumnCount.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getColumnCount.

Returns the number of columns in the model.

getIdentifierback to summary
public Integer getIdentifier(int index)

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getIdentifier.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getIdentifier.

Returns the identifier for the specified row. The return value of this is used as the identifier for the RowFilter.Entry that is passed to the RowFilter.

getModelback to summary
public M getModel()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getModel.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getModel.

Returns the underlying model that this Model is wrapping.

getRowCountback to summary
public int getRowCount()

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getRowCount.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getRowCount.

Returns the number of rows in the model.

getStringValueAtback to summary
public String getStringValueAt(int row, int column)

Overrides javax.swing.DefaultRowSorter.ModelWrapper.getStringValueAt.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getStringValueAt.

Returns the value as a String at the specified index. This implementation uses toString on the result from getValueAt (making sure to return an empty string for null values). Subclasses that override this method should never return null.

getValueAtback to summary
public Object getValueAt(int row, int column)

Implements abstract javax.swing.DefaultRowSorter.ModelWrapper.getValueAt.

Doc from javax.swing.DefaultRowSorter.ModelWrapper.getValueAt.

Returns the value at the specified index.