- java.lang.Object
- 
- javax.swing.RowSorter<M>
- 
- javax.swing.DefaultRowSorter<M,I>
 
 
- 
- Type Parameters:
- M- the type of the model
- I- the type of the identifier passed to the- RowFilter
 - Direct Known Subclasses:
- TableRowSorter
 
 public abstract class DefaultRowSorter<M,I> extends RowSorter<M> An implementation ofRowSorterthat provides sorting and filtering around a grid-based data model. Beyond creating and installing aRowSorter, you very rarely need to interact with one directly. Refer toTableRowSorterfor a concrete implementation ofRowSorterforJTable.Sorting is done based on the current SortKeys, in order. If two objects are equal (theComparatorfor the column returns 0) the nextSortKeyis used. If noSortKeys remain or the order isUNSORTED, then the order of the rows in the model is used.Sorting of each column is done by way of a Comparatorthat you can specify using thesetComparatormethod. If aComparatorhas not been specified, theComparatorreturned byCollator.getInstance()is used on the results of callingtoStringon the underlying objects. TheComparatoris never passednull. Anullvalue is treated as occurring before a non-nullvalue, and twonullvalues are considered equal.If you specify a Comparatorthat casts its argument to a type other than that provided by the model, aClassCastExceptionwill be thrown when the data is sorted.In addition to sorting, DefaultRowSorterprovides the ability to filter rows. Filtering is done by way of aRowFilterthat is specified using thesetRowFiltermethod. 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 modelStructureChangedmethod 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 defaultComparators, see the concrete implementation (for example,TableRowSorter). The default sort order is unsorted (the same as the model), and columns are sortable by default.DefaultRowSorteris an abstract class. Concrete subclasses must provide access to the underlying data by invokingsetModelWrapper. ThesetModelWrappermethod must be invoked soon after the constructor is called, ideally from within the subclass's constructor. Undefined behavior will result if you use aDefaultRowSorterwithout specifying aModelWrapper.DefaultRowSorterhas two formal type parameters. The first type parameter corresponds to the class of the model, for exampleDefaultTableModel. The second type parameter corresponds to the class of the identifier passed to theRowFilter. Refer toTableRowSorterandRowFilterfor more details on the type parameters.- Since:
- 1.6
- See Also:
- TableRowSorter,- DefaultTableModel,- Collator
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description protected static classDefaultRowSorter.ModelWrapper<M,I>DefaultRowSorter.ModelWrapperis responsible for providing the data that gets sorted byDefaultRowSorter.- 
Nested classes/interfaces declared in class javax.swing.RowSorterRowSorter.SortKey
 
- 
 - 
Constructor SummaryConstructors Constructor Description DefaultRowSorter()Creates an emptyDefaultRowSorter.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description intconvertRowIndexToModel(int index)Returns the location ofindexin terms of the underlying model.intconvertRowIndexToView(int index)Returns the location ofindexin terms of the view.Comparator<?>getComparator(int column)Returns theComparatorfor the specified column.intgetMaxSortKeys()Returns the maximum number of sort keys.MgetModel()Returns the underlying model.protected DefaultRowSorter.ModelWrapper<M,I>getModelWrapper()Returns the model wrapper providing the data that is being sorted and filtered.RowFilter<? super M,? super I>getRowFilter()Returns the filter that determines which rows, if any, should be hidden from view.List<? extends RowSorter.SortKey>getSortKeys()Returns the current sort keys.booleangetSortsOnUpdates()Returns true if a sort should happen when the underlying model is updated; otherwise, returns false.booleanisSortable(int column)Returns true if the specified column is sortable; otherwise, false.voidrowsDeleted(int firstRow, int endRow)Invoked when rows have been deleted from the underlying model in the specified range (inclusive).voidrowsInserted(int firstRow, int endRow)Invoked when rows have been inserted into the underlying model in the specified range (inclusive).voidrowsUpdated(int firstRow, int endRow)Invoked when rows have been changed in the underlying model between the specified range (inclusive).voidrowsUpdated(int firstRow, int endRow, int column)Invoked when the column in the rows have been updated in the underlying model between the specified range.voidsetComparator(int column, Comparator<?> comparator)Sets theComparatorto use when sorting the specified column.voidsetMaxSortKeys(int max)Sets the maximum number of sort keys.protected voidsetModelWrapper(DefaultRowSorter.ModelWrapper<M,I> modelWrapper)Sets the model wrapper providing the data that is being sorted and filtered.voidsetRowFilter(RowFilter<? super M,? super I> filter)Sets the filter that determines which rows, if any, should be hidden from the view.voidsetSortable(int column, boolean sortable)Sets whether or not the specified column is sortable.voidsetSortKeys(List<? extends RowSorter.SortKey> sortKeys)Sets the sort keys.voidsetSortsOnUpdates(boolean sortsOnUpdates)If true, specifies that a sort should happen when the underlying model is updated (rowsUpdatedis invoked).voidsort()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.voidtoggleSortOrder(int column)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.protected booleanuseToString(int column)Returns whether or not to convert the value to a string before doing comparisons when sorting.- 
Methods declared in class javax.swing.RowSorteraddRowSorterListener, allRowsChanged, fireRowSorterChanged, fireSortOrderChanged, getModelRowCount, getViewRowCount, modelStructureChanged, removeRowSorterListener
 
- 
 
- 
- 
- 
Method Detail- 
setModelWrapperprotected final void setModelWrapper(DefaultRowSorter.ModelWrapper<M,I> modelWrapper) Sets the model wrapper providing the data that is being sorted and filtered.- Parameters:
- modelWrapper- the model wrapper responsible for providing the data that gets sorted and filtered
- Throws:
- IllegalArgumentException- if- modelWrapperis- null
 
 - 
getModelWrapperprotected final DefaultRowSorter.ModelWrapper<M,I> getModelWrapper() Returns the model wrapper providing the data that is being sorted and filtered.- Returns:
- the model wrapper responsible for providing the data that gets sorted and filtered
 
 - 
getModelpublic final M getModel() Returns the underlying model.
 - 
setSortablepublic void setSortable(int column, boolean sortable)Sets whether or not the specified column is sortable. The specified value is only checked whentoggleSortOrderis invoked. It is still possible to sort on a column that has been marked as unsortable by directly setting the sort keys. The default is true.- Parameters:
- column- the column to enable or disable sorting on, in terms of the underlying model
- sortable- whether or not the specified column is sortable
- Throws:
- IndexOutOfBoundsException- if- columnis outside the range of the model
- See Also:
- toggleSortOrder(int),- setSortKeys(java.util.List<? extends javax.swing.RowSorter.SortKey>)
 
 - 
isSortablepublic boolean isSortable(int column) Returns true if the specified column is sortable; otherwise, false.- Parameters:
- column- the column to check sorting for, in terms of the underlying model
- Returns:
- true if the column is sortable
- Throws:
- IndexOutOfBoundsException- if column is outside the range of the underlying model
 
 - 
setSortKeyspublic void setSortKeys(List<? extends RowSorter.SortKey> sortKeys) Sets the sort keys. This creates a copy of the suppliedList; subsequent changes to the suppliedListdo not effect thisDefaultRowSorter. If the sort keys have changed this triggers a sort.- Specified by:
- setSortKeysin class- RowSorter<M>
- Parameters:
- sortKeys- the new- SortKeys;- nullis a shorthand for specifying an empty list, indicating that the view should be unsorted
- Throws:
- IllegalArgumentException- if any of the values in- sortKeysare null or have a column index outside the range of the model
 
 - 
getSortKeyspublic List<? extends RowSorter.SortKey> getSortKeys() Returns the current sort keys. This returns an unmodifiablenon-null List. If you need to change the sort keys, make a copy of the returnedList, mutate the copy and invokesetSortKeyswith the new list.- Specified by:
- getSortKeysin class- RowSorter<M>
- Returns:
- the current sort order
 
 - 
setMaxSortKeyspublic 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 andsetMaxSortKeys(2)is invoked on it. The user clicks the header for column 1, causing the table rows to be sorted based on the items in column 1. Next, the user clicks the header for column 2, causing the table to be sorted based on the items in column 2; if any items in column 2 are equal, then those particular rows are ordered based on the items in column 1. In this case, we say that the rows are primarily sorted on column 2, and secondarily on column 1. If the user then clicks the header for column 3, then the items are primarily sorted on column 3 and secondarily sorted on column 2. Because the maximum number of sort keys has been set to 2 withsetMaxSortKeys, column 1 no longer has an effect on the order.The maximum number of sort keys is enforced by toggleSortOrder. You can specify more sort keys by invokingsetSortKeysdirectly and they will all be honored. However iftoggleSortOrderis subsequently invoked the maximum number of sort keys will be enforced. The default value is 3.- Parameters:
- max- the maximum number of sort keys
- Throws:
- IllegalArgumentException- if- max< 1
 
 - 
getMaxSortKeyspublic int getMaxSortKeys() Returns the maximum number of sort keys.- Returns:
- the maximum number of sort keys
 
 - 
setSortsOnUpdatespublic void setSortsOnUpdates(boolean sortsOnUpdates) If true, specifies that a sort should happen when the underlying model is updated (rowsUpdatedis invoked). For example, if this is true and the user edits an entry the location of that item in the view may change. The default is false.- Parameters:
- sortsOnUpdates- whether or not to sort on update events
 
 - 
getSortsOnUpdatespublic boolean getSortsOnUpdates() Returns true if a sort should happen when the underlying model is updated; otherwise, returns false.- Returns:
- whether or not to sort when the model is updated
 
 - 
setRowFilterpublic 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 ofnullindicates all values from the model should be included.RowFilter'sincludemethod is passed anEntrythat wraps the underlying model. The number of columns in theEntrycorresponds to the number of columns in theModelWrapper. The identifier comes from theModelWrapperas well.This method triggers a sort. - Parameters:
- filter- the filter used to determine what entries should be included
 
 - 
getRowFilterpublic RowFilter<? super M,? super I> getRowFilter() Returns the filter that determines which rows, if any, should be hidden from view.- Returns:
- the filter
 
 - 
toggleSortOrderpublic void toggleSortOrder(int column) 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.- Specified by:
- toggleSortOrderin class- RowSorter<M>
- Parameters:
- column- index of the column to make the primary sorted column, in terms of the underlying model
- Throws:
- IndexOutOfBoundsException- if column is outside the range of the underlying model
- See Also:
- setSortable(int,boolean),- setMaxSortKeys(int)
 
 - 
convertRowIndexToViewpublic int convertRowIndexToView(int index) Returns the location ofindexin terms of the view. That is, for the rowindexin the coordinates of the underlying model this returns the row index in terms of the view.- Specified by:
- convertRowIndexToViewin class- RowSorter<M>
- Parameters:
- index- the row index in terms of the underlying model
- Returns:
- row index in terms of the view, or -1 if index has been filtered out of the view
- Throws:
- IndexOutOfBoundsException- if- indexis outside the range of the model
 
 - 
convertRowIndexToModelpublic int convertRowIndexToModel(int index) Returns the location ofindexin terms of the underlying model. That is, for the rowindexin the coordinates of the view this returns the row index in terms of the underlying model.- Specified by:
- convertRowIndexToModelin class- RowSorter<M>
- Parameters:
- index- the row index in terms of the underlying view
- Returns:
- row index in terms of the view
- Throws:
- IndexOutOfBoundsException- if- indexis outside the range of the view
 
 - 
sortpublic 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 emptysortKeyslist indicates that the view should unsorted, the same as the model.
 - 
useToStringprotected boolean useToString(int column) Returns whether or not to convert the value to a string before doing comparisons when sorting. If trueModelWrapper.getStringValueAtwill be used, otherwiseModelWrapper.getValueAtwill be used. It is up to subclasses, such asTableRowSorter, to honor this value in theirModelWrapperimplementation.- 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- if- columnis not valid
 
 - 
setComparatorpublic void setComparator(int column, Comparator<?> comparator)Sets theComparatorto use when sorting the specified column. This does not trigger a sort. If you want to sort after setting the comparator you need to explicitly invokesort.- Parameters:
- column- the index of the column the- Comparatoris to be used for, in terms of the underlying model
- comparator- the- Comparatorto use
- Throws:
- IndexOutOfBoundsException- if- columnis outside the range of the underlying model
 
 - 
getComparatorpublic Comparator<?> getComparator(int column) Returns theComparatorfor the specified column. This will returnnullif aComparatorhas not been specified for the column.- Parameters:
- column- the column to fetch the- Comparatorfor, in terms of the underlying model
- Returns:
- the Comparatorfor the specified column
- Throws:
- IndexOutOfBoundsException- if column is outside the range of the underlying model
 
 - 
rowsInsertedpublic void rowsInserted(int firstRow, int endRow)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. - Specified by:
- rowsInsertedin class- RowSorter<M>
- Parameters:
- firstRow- the first row
- endRow- the last row
- Throws:
- IndexOutOfBoundsException- if either argument is invalid, or- firstRow>- endRow
 
 - 
rowsDeletedpublic void rowsDeleted(int firstRow, int endRow)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. - Specified by:
- rowsDeletedin class- RowSorter<M>
- Parameters:
- firstRow- the first row
- endRow- the last row
- Throws:
- IndexOutOfBoundsException- if either argument is outside the range of the model before the change, or- firstRow>- endRow
 
 - 
rowsUpdatedpublic void rowsUpdated(int firstRow, int endRow)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. - Specified by:
- rowsUpdatedin class- RowSorter<M>
- Parameters:
- firstRow- the first row, in terms of the underlying model
- endRow- the last row, in terms of the underlying model
- Throws:
- IndexOutOfBoundsException- if either argument is outside the range of the underlying model, or- firstRow>- endRow
 
 - 
rowsUpdatedpublic void rowsUpdated(int firstRow, int endRow, int column)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. - Specified by:
- rowsUpdatedin class- RowSorter<M>
- Parameters:
- firstRow- the first row, in terms of the underlying model
- endRow- the last row, in terms of the underlying model
- column- the column that has changed, in terms of the underlying model
- Throws:
- IndexOutOfBoundsException- if either argument is outside the range of the underlying model after the change,- firstRow>- endRow, or- columnis outside the range of the underlying model
 
 
- 
 
-