Class TableRowSorter<M extends TableModel>
- Type Parameters:
M- the type of the model, which must be an implementation ofTableModel
public class TableRowSorter<M extends TableModel> extends DefaultRowSorter<M,Integer>
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:
- If a
Comparatorhas been specified for the column by thesetComparatormethod, use it. - If the column class as returned by
getColumnClassisString, use theComparatorreturned byCollator.getInstance(). - If the column class implements
Comparable, use aComparatorthat invokes thecompareTomethod. - If a
TableStringConverterhas been specified, use it to convert the values toStrings and then use theComparatorreturned byCollator.getInstance(). - Otherwise use the
Comparatorreturned byCollator.getInstance()on the results from callingtoStringon 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:
Nested Class Summary
Nested classes/interfaces declared in class DefaultRowSorter
DefaultRowSorter.ModelWrapper<M,I> | Modifier and Type | Class | Description |
|---|---|---|
protected static class |
DefaultRowSorter.ModelWrapper<M, |
DefaultRowSorter.ModelWrapper is responsible for providing the data that gets sorted by DefaultRowSorter. |
Nested classes/interfaces declared in class RowSorter
RowSorter.SortKey | Modifier and Type | Class | Description |
|---|---|---|
static class |
RowSorter.SortKey |
SortKey describes the sort order for a particular column. |
Constructor Summary
| Constructor | Description |
|---|---|
TableRowSorter() |
Creates a TableRowSorter with an empty model. |
TableRowSorter |
Creates a TableRowSorter using model as the underlying TableModel. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
Comparator |
getComparator |
Returns the Comparator for the specified column. |
TableStringConverter |
getStringConverter() |
Returns the object responsible for converting values from the model to strings. |
void |
setModel |
Sets the TableModel to use as the underlying model for this TableRowSorter. |
void |
setStringConverter |
Sets the object responsible for converting values from the model to strings. |
protected boolean |
useToString |
Returns whether or not to convert the value to a string before doing comparisons when sorting. |
Methods declared in class DefaultRowSorter
allRowsChanged, convertRowIndexToModel, convertRowIndexToView, getMaxSortKeys, getModel, getModelRowCount, getModelWrapper, getRowFilter, getSortKeys, getSortsOnUpdates, getViewRowCount, isSortable, modelStructureChanged, rowsDeleted, rowsInserted, rowsUpdated, rowsUpdated, setComparator, setMaxSortKeys, setModelWrapper, setRowFilter, setSortable, setSortKeys, setSortsOnUpdates, sort, toggleSortOrder | Modifier and Type | Method | Description |
|---|---|---|
void |
allRowsChanged() |
Invoked when the contents of the underlying model have completely changed. |
int |
convertRowIndexToModel |
Returns the location of index in terms of the underlying model. |
int |
convertRowIndexToView |
Returns the location of index in terms of the view. |
int |
getMaxSortKeys() |
Returns the maximum number of sort keys. |
final M |
getModel() |
Returns the underlying model. |
int |
getModelRowCount() |
Returns the number of rows in the underlying model. |
protected final DefaultRowSorter.ModelWrapper |
getModelWrapper() |
Returns the model wrapper providing the data that is being sorted and filtered. |
RowFilter |
getRowFilter() |
Returns the filter that determines which rows, if any, should be hidden from view. |
List |
getSortKeys() |
Returns the current sort keys. |
boolean |
getSortsOnUpdates() |
Returns true if a sort should happen when the underlying model is updated; otherwise, returns false. |
int |
getViewRowCount() |
Returns the number of rows in the view. |
boolean |
isSortable |
Returns true if the specified column is sortable; otherwise, false. |
void |
modelStructureChanged() |
Invoked when the underlying model structure has completely changed. |
void |
rowsDeleted |
Invoked when rows have been deleted from the underlying model in the specified range (inclusive). |
void |
rowsInserted |
Invoked when rows have been inserted into the underlying model in the specified range (inclusive). |
void |
rowsUpdated |
Invoked when rows have been changed in the underlying model between the specified range (inclusive). |
void |
rowsUpdated |
Invoked when the column in the rows have been updated in the underlying model between the specified range. |
void |
setComparator |
Sets the Comparator to use when sorting the specified column. |
void |
setMaxSortKeys |
Sets the maximum number of sort keys. |
protected final void |
setModelWrapper |
Sets the model wrapper providing the data that is being sorted and filtered. |
void |
setRowFilter |
Sets the filter that determines which rows, if any, should be hidden from the view. |
void |
setSortable |
Sets whether or not the specified column is sortable. |
void |
setSortKeys |
Sets the sort keys. |
void |
setSortsOnUpdates |
If true, specifies that a sort should happen when the underlying model is updated ( rowsUpdated is invoked). |
void |
sort() |
Sorts and filters the rows in the view based on the sort keys of the columns currently being sorted and the filter, if any, associated with this sorter. |
void |
toggleSortOrder |
Reverses the sort order from ascending to descending (or descending to ascending) if the specified column is already the primary sorted column; otherwise, makes the specified column the primary sorted column, with an ascending sort order. |
Methods declared in class RowSorter
addRowSorterListener, fireRowSorterChanged, fireSortOrderChanged, removeRowSorterListener | Modifier and Type | Method | Description |
|---|---|---|
void |
addRowSorterListener |
Adds a RowSorterListener to receive notification about this RowSorter. |
protected void |
fireRowSorterChanged |
Notifies listener that the mapping has changed. |
protected void |
fireSortOrderChanged() |
Notifies listener that the sort order has changed. |
void |
removeRowSorterListener |
Removes a RowSorterListener. |
Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait | Modifier and Type | Method | Description |
|---|---|---|
protected Object |
clone() |
Creates and returns a copy of this object. |
boolean |
equals |
Indicates whether some other object is "equal to" this one. |
protected void |
finalize() |
Deprecated, for removal: This API element is subject to removal in a future version. Finalization is deprecated and subject to removal in a future release. |
final Class |
getClass() |
Returns the runtime class of this Object. |
int |
hashCode() |
Returns a hash code value for this object. |
final void |
notify() |
Wakes up a single thread that is waiting on this object's monitor. |
final void |
notifyAll() |
Wakes up all threads that are waiting on this object's monitor. |
String |
toString() |
Returns a string representation of the object. |
final void |
wait() |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
final void |
wait |
Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. |
Constructor Details
TableRowSorter
public TableRowSorter()
TableRowSorter with an empty model.TableRowSorter
public TableRowSorter(M model)
TableRowSorter using model as the underlying TableModel.- Parameters:
-
model- the underlyingTableModelto use,nullis treated as an empty model
Method Details
setModel
public void setModel(M model)
TableModel to use as the underlying model for this TableRowSorter. A value of null can be used to set an empty model.- Parameters:
-
model- the underlying model to use, ornull
setStringConverter
public void setStringConverter(TableStringConverter stringConverter)
null this is used to convert any object values, that do not have a registered Comparator, to strings.- Parameters:
-
stringConverter- the object responsible for converting values from the model to strings
getStringConverter
public TableStringConverter getStringConverter()
- Returns:
- object responsible for converting values to strings.
getComparator
public Comparator<?> getComparator(int column)
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.- Overrides:
-
getComparatorin classDefaultRowSorter<M extends TableModel, Integer> - Parameters:
-
column- the column to fetch theComparatorfor, in terms of the underlying model - Returns:
- the
Comparatorfor the specified column - Throws:
-
IndexOutOfBoundsException- if column is outside the range of the underlying model
useToString
protected boolean useToString(int column)
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.- Overrides:
-
useToStringin classDefaultRowSorter<M extends TableModel, Integer> - Parameters:
-
column- the index of the column to test, in terms of the underlying model - Returns:
- true if values are to be converted to strings before doing comparisons when sorting
- Throws:
-
IndexOutOfBoundsException- ifcolumnis not valid
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.