RowFilter
RowSorter that provides sorting and
filtering around a grid-based data model.
Beyond creating and installing a RowSorter, you very rarely
need to interact with one directly. Refer to
TableRowSorter for a concrete
implementation of RowSorter for JTable.
Sorting is done based on the current SortKeys, in order.
If two objects are equal (the Comparator for the
column returns 0) the next SortKey is used. If no
SortKeys remain or the order is UNSORTED, then
the order of the rows in the model is used.
Sorting of each column is done by way of a Comparator
that you can specify using the setComparator method.
If a Comparator has not been specified, the
Comparator returned by
Collator.getInstance() is used on the results of
calling toString on the underlying objects. The
Comparator is never passed null. A
null value is treated as occurring before a
non-null value, and two null values are
considered equal.
If you specify a Comparator that casts its argument to
a type other than that provided by the model, a
ClassCastException will be thrown when the data is sorted.
In addition to sorting, DefaultRowSorter provides the
ability to filter rows. Filtering is done by way of a
RowFilter that is specified using the
setRowFilter method. If no filter has been specified all
rows are included.
By default, rows are in unsorted order (the same as the model) and
every column is sortable. The default Comparators are
documented in the subclasses (for example, TableRowSorter).
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. To
find the default Comparators, see the concrete
implementation (for example, TableRowSorter). The default
sort order is unsorted (the same as the model), and columns are
sortable by default.
DefaultRowSorter is an abstract class. Concrete
subclasses must provide access to the underlying data by invoking
setModelWrapper. The setModelWrapper method
must be invoked soon after the constructor is
called, ideally from within the subclass's constructor.
Undefined behavior will result if you use a DefaultRowSorter without specifying a ModelWrapper.
DefaultRowSorter has two formal type parameters. The
first type parameter corresponds to the class of the model, for example
DefaultTableModel. The second type parameter
corresponds to the class of the identifier passed to the
RowFilter. Refer to TableRowSorter and
RowFilter for more details on the type parameters.
javax.swing.table.TableRowSorter , javax.swing.table.DefaultTableModel , java.text.Collator
| Modifier and Type | Class and Description |
|---|---|
| private class | DefaultRowSorter.
RowFilter.Entry implementation that delegates to the ModelWrapper. |
| protected abstract static class | DefaultRowSorter.
the type of the underlying model M, the identifier supplied to the filter I>
|
| private static class | DefaultRowSorter.
Row is used to handle the actual sorting by way of Comparable. |
| Modifier and Type | Field and Description |
|---|---|
| private RowSorter. | cachedSortKeys
Cached SortKeys for the current sort. |
| private Comparator | comparators
Comparators specified by column. |
| private RowFilter | filter
Developer supplied Filter. |
| private DefaultRowSorter | filterEntry
Value passed to the filter. |
| private boolean[] | isSortable
Whether or not the specified column is sortable, by column. |
| private int | maxSortKeys
Maximum number of sort keys. |
| private int | modelRowCount
Size of the model. |
| private int[] | modelToView
model -> view (JTable) |
| private DefaultRowSorter. | modelWrapper
Provides access to the data we're sorting/filtering. |
| private Comparator | sortComparators
Cached comparators for the current sort |
| private boolean | sorted
Indicates the contents are sorted. |
| private List | sortKeys
The sort keys. |
| private boolean | sortsOnUpdates
Whether or not we resort on TableModelEvent.UPDATEs. |
| private boolean[] | useToString
Whether or not to use getStringValueAt. |
| private DefaultRowSorter. | viewToModel
View (JTable) -> model. |
| private static boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public void | allRowsChanged()
Implements abstract javax. Invoked when the contents of the underlying model have completely changed. |
| private void | |
| private void | |
| private void | |
| private int | |
| public int | convertRowIndexToModel(int index)
Implements abstract javax. Returns the location of |
| public int | convertRowIndexToView(int index)
Implements abstract javax. Returns the location of |
| private int | |
| private void | |
| private void | |
| public Comparator | Returns: theComparator for the specified columnthe column to fetch the column)Comparator for, in
terms of the underlying modelReturns the |
| private Comparator | |
| private RowFilter. | |
| public int | |
| public final M | Returns: the underlying modelImplements abstract javax. Returns the underlying model. |
| public int | getModelRowCount()
Implements abstract javax. Returns the number of rows in the underlying model. |
| protected final DefaultRowSorter. | Returns: the model wrapper responsible for providing the data that gets sorted and filteredReturns the model wrapper providing the data that is being sorted and filtered. |
| public RowFilter | Returns: the filterReturns the filter that determines which rows, if any, should be hidden from view. |
| public List | Returns: the current sort orderImplements abstract javax. Returns the current sort keys. |
| public boolean | Returns: whether or not to sort when the model is updatedReturns true if a sort should happen when the underlying model is updated; otherwise, returns false. |
| public int | getViewRowCount()
Implements abstract javax. Returns the number of rows in the view. |
| private int[] | |
| private boolean | |
| private void | initializeFilteredMapping()
Resets the viewToModel and modelToView mappings based on the current Filter. |
| private void | insertInOrder(List<DefaultRowSorter.
the Rows to add, sorted toAdd,the array to insert the items into currentInsets new set of entries. |
| public boolean | Returns: true if the column is sortablethe column to check sorting for, in terms of the
underlying model column)Returns true if the specified column is sortable; otherwise, false. |
| private boolean | |
| private boolean | |
| public void | modelStructureChanged()
Implements abstract javax. Invoked when the underlying model structure has completely changed. |
| public void | rowsDeleted(int firstRow, int endRow)
Implements abstract javax. Invoked when rows have been deleted from the underlying model in the specified range (inclusive). |
| private void | |
| public void | rowsInserted(int firstRow, int endRow)
Implements abstract javax. Invoked when rows have been inserted into the underlying model in the specified range (inclusive). |
| private void | |
| public void | rowsUpdated(int firstRow, int endRow)
Implements abstract javax. Invoked when rows have been changed in the underlying model between the specified range (inclusive). |
| public void | rowsUpdated(int firstRow, int endRow, int column)
Implements abstract javax. Invoked when the column in the rows have been updated in the underlying model between the specified range. |
| private void | |
| public void | setComparator(int
the index of the column the column, Comparator<?> Comparator is
to be used for, in terms of the underlying modelthe comparator)Comparator to useSets the |
| public void | |
| private void | setModelToViewFromViewToModel(boolean unsetFirst)
Refreshes the modelToView mapping from that of viewToModel. |
| protected final void | setModelWrapper(DefaultRowSorter.
the model wrapper responsible for providing the
data that gets sorted and filtered modelWrapperSets the model wrapper providing the data that is being sorted and filtered. |
| public void | setRowFilter(RowFilter<? super M, ? super I>
the filter used to determine what entries should be
included filter)Sets the filter that determines which rows, if any, should be hidden from the view. |
| public void | setSortable(int
the column to enable or disable sorting on, in terms
of the underlying model column, boolean whether or not the specified column is sortable sortable)Sets whether or not the specified column is sortable. |
| public void | setSortKeys(List<? extends RowSorter.
the new sortKeys)SortKeys; null
is a shorthand for specifying an empty list,
indicating that the view should be unsortedImplements abstract javax. Sets the sort keys. |
| public void | setSortsOnUpdates(boolean
whether or not to sort on update events sortsOnUpdates)If true, specifies that a sort should happen when the underlying
model is updated ( |
| private boolean | shouldOptimizeChange(int firstRow, int lastRow)
Returns true if we should try and optimize the processing of the
|
| public 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. |
| private void | |
| private RowSorter. | |
| public void | toggleSortOrder(int
index of the column to make the primary sorted column,
in terms of the underlying model column)Implements abstract javax. 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. |
| private void | |
| protected boolean | Returns: true if values are to be converted to strings before doing comparisons when sortingthe index of the column to test, in terms of the
underlying model column)Returns whether or not to convert the value to a string before doing comparisons when sorting. |
| cachedSortKeys | back to summary |
|---|---|
| private RowSorter. Cached SortKeys for the current sort. | |
| comparators | back to summary |
|---|---|
| private Comparator<?>[] comparators Comparators specified by column. | |
| filter | back to summary |
|---|---|
| private RowFilter<? super M, ? super I> filter Developer supplied Filter. | |
| filterEntry | back to summary |
|---|---|
| private DefaultRowSorter<M, I>. Value passed to the filter. The same instance is passed to the filter for different rows. | |
| isSortable | back to summary |
|---|---|
| private boolean[] isSortable Whether or not the specified column is sortable, by column. | |
| maxSortKeys | back to summary |
|---|---|
| private int maxSortKeys Maximum number of sort keys. | |
| modelRowCount | back to summary |
|---|---|
| private int modelRowCount Size of the model. This is used to enforce error checking within the table changed notification methods (such as rowsInserted). | |
| modelToView | back to summary |
|---|---|
| private int[] modelToView model -> view (JTable) | |
| modelWrapper | back to summary |
|---|---|
| private DefaultRowSorter. Provides access to the data we're sorting/filtering. | |
| sortComparators | back to summary |
|---|---|
| private Comparator<?>[] sortComparators Cached comparators for the current sort | |
| sorted | back to summary |
|---|---|
| private boolean sorted Indicates the contents are sorted. This is used if getSortsOnUpdates is false and an update event is received. | |
| sortKeys | back to summary |
|---|---|
| private List<RowSorter. The sort keys. | |
| sortsOnUpdates | back to summary |
|---|---|
| private boolean sortsOnUpdates Whether or not we resort on TableModelEvent.UPDATEs. | |
| useToString | back to summary |
|---|---|
| private boolean[] useToString Whether or not to use getStringValueAt. This is indexed by column. | |
| viewToModel | back to summary |
|---|---|
| private DefaultRowSorter. View (JTable) -> model. | |
| warning8160087 | back to summary |
|---|---|
| private static boolean warning8160087 | |
| DefaultRowSorter | back to summary |
|---|---|
| public DefaultRowSorter() Creates an empty | |
| allChanged | back to summary |
|---|---|
| private void allChanged() | |
| allRowsChanged | back to summary |
|---|---|
| public void allRowsChanged() Implements abstract javax. Doc from javax. Invoked when the contents of the underlying model have completely changed. The structure of the table is the same, only the contents have changed. This is typically sent when it is too expensive to characterize the change in terms of the other methods. You normally do not call this method. This method is public to allow view classes to call it. | |
| cacheSortKeys | back to summary |
|---|---|
| private void cacheSortKeys(List<? extends RowSorter. Caches the sort keys before a sort. | |
| checkAgainstModel | back to summary |
|---|---|
| private void checkAgainstModel(int firstRow, int endRow) | |
| checkColumn | back to summary |
|---|---|
| private void checkColumn(int column) | |
| compare | back to summary |
|---|---|
private int compare(int model1, int model2)
| |
| convertRowIndexToModel | back to summary |
|---|---|
| public int convertRowIndexToModel(int index) Implements abstract javax. Doc from javax. Returns the location of
| |
| convertRowIndexToView | back to summary |
|---|---|
| public int convertRowIndexToView(int index) Implements abstract javax. Doc from javax. Returns the location of
| |
| convertUnsortedUnfiltered | back to summary |
|---|---|
| private int convertUnsortedUnfiltered(int index) | |
| createModelToView | back to summary |
|---|---|
| private void createModelToView(int rowCount) Makes sure the modelToView array is of size rowCount. | |
| createViewToModel | back to summary |
|---|---|
| private void createViewToModel(int rowCount) Resets the viewToModel array to be of size rowCount. | |
| getComparator | back to summary |
|---|---|
| public Comparator Returns the
| |
| getComparator0 | back to summary |
|---|---|
| private Comparator | |
| getFilterEntry | back to summary |
|---|---|
| private RowFilter. | |
| getMaxSortKeys | back to summary |
|---|---|
| public int getMaxSortKeys() Returns the maximum number of sort keys.
| |
| getModel | back to summary |
|---|---|
| public final M getModel() Implements abstract javax. Returns the underlying model.
| |
| getModelRowCount | back to summary |
|---|---|
| public int getModelRowCount() Implements abstract javax. Doc from javax. Returns the number of rows in the underlying model. | |
| getModelWrapper | back to summary |
|---|---|
| protected final DefaultRowSorter. Returns the model wrapper providing the data that is being sorted and filtered.
| |
| getRowFilter | back to summary |
|---|---|
| public RowFilter Returns the filter that determines which rows, if any, should be hidden from view.
| |
| getSortKeys | back to summary |
|---|---|
| public List Implements abstract javax. Returns the current sort keys. This returns an unmodifiable
| |
| getSortsOnUpdates | back to summary |
|---|---|
| public boolean getSortsOnUpdates() Returns true if a sort should happen when the underlying model is updated; otherwise, returns false.
| |
| getViewRowCount | back to summary |
|---|---|
| public int getViewRowCount() Implements abstract javax. Doc from javax. Returns the number of rows in the view. If the contents have been filtered this might differ from the row count of the underlying model. | |
| getViewToModelAsInts | back to summary |
|---|---|
| private int[] getViewToModelAsInts(DefaultRowSorter. | |
| include | back to summary |
|---|---|
| private boolean include(int row) Returns true if the specified row should be included. | |
| initializeFilteredMapping | back to summary |
|---|---|
| private void initializeFilteredMapping() Resets the viewToModel and modelToView mappings based on the current Filter. | |
| insertInOrder | back to summary |
|---|---|
| private void insertInOrder(List<DefaultRowSorter. Insets new set of entries.
| |
| isSortable | back to summary |
|---|---|
| public boolean isSortable(int column) Returns true if the specified column is sortable; otherwise, false.
| |
| isTransformed | back to summary |
|---|---|
| private boolean isTransformed() Whether not we are filtering/sorting. | |
| isUnsorted | back to summary |
|---|---|
| private boolean isUnsorted() | |
| modelStructureChanged | back to summary |
|---|---|
| public void modelStructureChanged() Implements abstract javax. Doc from javax. Invoked when the underlying model structure has completely
changed. For example, if the number of columns in a
You normally do not call this method. This method is public to allow view classes to call it. | |
| rowsDeleted | back to summary |
|---|---|
| public void rowsDeleted(int firstRow, int endRow) Implements abstract javax. Doc from javax. Invoked when rows have been deleted from the underlying model in the specified range (inclusive). The arguments give the indices of the effected range and are in terms of the model before the change. For example, if you have a 5-row model and delete 3 items from the end of the model the indices are 2, 4. You normally do not call this method. This method is public to allow view classes to call it.
| |
| rowsDeleted0 | back to summary |
|---|---|
| private void rowsDeleted0(int firstRow, int lastRow) | |
| rowsInserted | back to summary |
|---|---|
| public void rowsInserted(int firstRow, int endRow) Implements abstract javax. Doc from javax. Invoked when rows have been inserted into the underlying model in the specified range (inclusive). The arguments give the indices of the effected range. The first argument is in terms of the model before the change, and must be less than or equal to the size of the model before the change. The second argument is in terms of the model after the change and must be less than the size of the model after the change. For example, if you have a 5-row model and add 3 items to the end of the model the indices are 5, 7. You normally do not call this method. This method is public to allow view classes to call it.
| |
| rowsInserted0 | back to summary |
|---|---|
| private void rowsInserted0(int firstRow, int lastRow) | |
| rowsUpdated | back to summary |
|---|---|
| public void rowsUpdated(int firstRow, int endRow) Implements abstract javax. Doc from javax. Invoked when rows have been changed in the underlying model between the specified range (inclusive). You normally do not call this method. This method is public to allow view classes to call it.
| |
| rowsUpdated | back to summary |
|---|---|
| public void rowsUpdated(int firstRow, int endRow, int column) Implements abstract javax. Doc from javax. Invoked when the column in the rows have been updated in the underlying model between the specified range. You normally do not call this method. This method is public to allow view classes to call it.
| |
| rowsUpdated0 | back to summary |
|---|---|
| private void rowsUpdated0(int firstRow, int lastRow) | |
| setComparator | back to summary |
|---|---|
| public void setComparator(int column, Comparator<?> comparator) Sets the
| |
| setMaxSortKeys | back to summary |
|---|---|
| public void setMaxSortKeys(int max) Sets the maximum number of sort keys. The number of sort keys
determines how equal values are resolved when sorting. For
example, assume a table row sorter is created and
The maximum number of sort keys is enforced by
| |
| setModelToViewFromViewToModel | back to summary |
|---|---|
| private void setModelToViewFromViewToModel(boolean unsetFirst) Refreshes the modelToView mapping from that of viewToModel.
If | |
| setModelWrapper | back to summary |
|---|---|
| protected final void setModelWrapper(DefaultRowSorter. Sets the model wrapper providing the data that is being sorted and filtered.
| |
| setRowFilter | back to summary |
|---|---|
| public void setRowFilter(RowFilter<? super M, ? super I> filter) Sets the filter that determines which rows, if any, should be
hidden from the view. The filter is applied before sorting. A value
of
This method triggers a sort.
| |
| setSortable | back to summary |
|---|---|
| public void setSortable(int column, boolean sortable) Sets whether or not the specified column is sortable. The specified
value is only checked when
| |
| setSortKeys | back to summary |
|---|---|
| public void setSortKeys(List<? extends RowSorter. Implements abstract javax. Sets the sort keys. This creates a copy of the supplied
| |
| setSortsOnUpdates | back to summary |
|---|---|
| public void setSortsOnUpdates(boolean sortsOnUpdates) If true, specifies that a sort should happen when the underlying
model is updated (
| |
| shouldOptimizeChange | back to summary |
|---|---|
| private boolean shouldOptimizeChange(int firstRow, int lastRow) Returns true if we should try and optimize the processing of the
| |
| sort | back to summary |
|---|---|
| public 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. An empty
| |
| sortExistingData | back to summary |
|---|---|
| private void sortExistingData() Sorts the existing filtered data. This should only be used if the filter hasn't changed. | |
| toggle | back to summary |
|---|---|
| private RowSorter. | |
| toggleSortOrder | back to summary |
|---|---|
| public void toggleSortOrder(int column) Implements abstract javax. 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. If the specified column is not sortable, this method has no effect.
| |
| updateUseToString | back to summary |
|---|---|
| private void updateUseToString() Updates the useToString mapping before a sort. | |
| useToString | back to summary |
|---|---|
| protected boolean useToString(int column) Returns whether or not to convert the value to a string before
doing comparisons when sorting. If true
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | modelIndex
The index into the model, set in getFilterEntry |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public I | getIdentifier()
Implements abstract javax. Returns the identifier (in the model) of the entry. |
| public M | |
| public String | getStringValue(int index)
Overrides javax. Returns the string value at the specified index. |
| public Object | getValue(int index)
Implements abstract javax. Returns the value at the specified index. |
| public int | getValueCount()
Implements abstract javax. Returns the number of values in the entry. |
| modelIndex | back to summary |
|---|---|
| pack-priv int modelIndex The index into the model, set in getFilterEntry | |
| FilterEntry | back to summary |
|---|---|
| private FilterEntry() | |
| getIdentifier | back to summary |
|---|---|
| public I getIdentifier() Implements abstract javax. Doc from javax. Returns the identifier (in the model) of the entry.
For a table this corresponds to the index of the row in the model,
expressed as an | |
| getModel | back to summary |
|---|---|
| public M getModel() Implements abstract javax. Doc from javax. Returns the underlying model. | |
| getStringValue | back to summary |
|---|---|
| public String getStringValue(int index) Overrides javax. Doc from javax. Returns the string value at the specified index. If
filtering is being done based on
This implementation calls | |
| getValue | back to summary |
|---|---|
| public Object getValue(int index) Implements abstract javax. Doc from javax. Returns the value at the specified index. This may return
| |
| getValueCount | back to summary |
|---|---|
| public int getValueCount() Implements abstract javax. Doc from javax. Returns the number of values in the entry. For example, when used with a table this corresponds to the number of columns. | |
DefaultRowSorter.ModelWrapper is responsible for providing
the data that gets sorted by DefaultRowSorter. You
normally do not interact directly with ModelWrapper.
Subclasses of DefaultRowSorter provide an
implementation of ModelWrapper wrapping another model.
For example,
TableRowSorter provides a ModelWrapper that
wraps a TableModel.
ModelWrapper makes a distinction between values as
Objects and Strings. This allows
implementations to provide a custom string
converter to be used instead of invoking toString on the
object.
RowFilter, RowFilter.Entry
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract int | Returns: the number of columns in the modelReturns the number of columns in the model. |
| public abstract I | Returns: the identifierthe row to return the identifier for, in terms of
the underlying model row)Returns the identifier for the specified row. |
| public abstract M | |
| public abstract int | |
| public String | Returns: the value at the specified index as aStringthe row index row, int the column index column)Returns the value as a |
| public abstract Object | Returns: the value at the specified indexthe row index row, int the column index column)Returns the value at the specified index. |
| ModelWrapper | back to summary |
|---|---|
| protected ModelWrapper() Creates a new | |
| getColumnCount | back to summary |
|---|---|
| public abstract int getColumnCount() Returns the number of columns in the model.
| |
| getIdentifier | back to summary |
|---|---|
| public abstract I getIdentifier(int row) Returns the identifier for the specified row. The return value
of this is used as the identifier for the
| |
| getModel | back to summary |
|---|---|
| public abstract M getModel() Returns the underlying model that this
| |
| getRowCount | back to summary |
|---|---|
| public abstract int getRowCount() Returns the number of rows in the model.
| |
| getStringValueAt | back to summary |
|---|---|
| public String getStringValueAt(int row, int column) Returns the value as a
| |
| getValueAt | back to summary |
|---|---|
| public abstract Object getValueAt(int row, int column) Returns the value at the specified index.
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| private DefaultRowSorter |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(DefaultRowSorter.
Implements java. Compares this object with the specified object for order. |
| modelIndex | back to summary |
|---|---|
| pack-priv int modelIndex | |
| sorter | back to summary |
|---|---|
| private DefaultRowSorter<?, ?> sorter | |
| Row | back to summary |
|---|---|
| public Row(DefaultRowSorter<?, ?> sorter, int index) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(DefaultRowSorter. Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that | |