- java.lang.Object
-
- javax.swing.RowSorter<M>
-
- Type Parameters:
M- the type of the underlying model
- Direct Known Subclasses:
DefaultRowSorter
public abstract class RowSorter<M> extends Object
RowSorterprovides the basis for sorting and filtering. Beyond creating and installing aRowSorter, you very rarely need to interact with one directly. Refer toTableRowSorterfor a concrete implementation ofRowSorterforJTable.RowSorter's primary role is to provide a mapping between two coordinate systems: that of the view (for example aJTable) and that of the underlying data source, typically a model.The view invokes the following methods on the
RowSorter:toggleSortOrder— The view invokes this when the appropriate user gesture has occurred to trigger a sort. For example, the user clicked a column header in a table.- One of the model change methods — The view invokes a model
change method when the underlying model
has changed. There may be order dependencies in how the events are
delivered, so a
RowSortershould not update its mapping until one of these methods is invoked.
convertRowIndexToModel,convertRowIndexToViewandgetViewRowCountmethods, these methods need to be fast.RowSorterprovides notification of changes by way ofRowSorterListener. Two types of notification are sent:RowSorterEvent.Type.SORT_ORDER_CHANGED— notifies listeners that the sort order has changed. This is typically followed by a notification that the sort has changed.RowSorterEvent.Type.SORTED— notifies listeners that the mapping maintained by theRowSorterhas changed in some way.
RowSorterimplementations typically don't have a one-to-one mapping with the underlying model, but they can. For example, if a database does the sorting,toggleSortOrdermight call through to the database (on a background thread), and override the mapping methods to return the argument that is passed in.Concrete implementations of
RowSorterneed to reference a model such asTableModelorListModel. The view classes, such asJTableandJList, will also have a reference to the model. To avoid ordering dependencies,RowSorterimplementations should not install a listener on the model. Instead the view class will call into theRowSorterwhen the model changes. For example, if a row is updated in aTableModelJTableinvokesrowsUpdated. When the model changes, the view may call into any of the following methods:modelStructureChanged,allRowsChanged,rowsInserted,rowsDeletedandrowsUpdated.- Since:
- 1.6
- See Also:
TableRowSorter
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRowSorter.SortKeySortKey describes the sort order for a particular column.
-
Constructor Summary
Constructors Constructor Description RowSorter()Creates aRowSorter.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddRowSorterListener(RowSorterListener l)Adds aRowSorterListenerto receive notification about thisRowSorter.abstract voidallRowsChanged()Invoked when the contents of the underlying model have completely changed.abstract intconvertRowIndexToModel(int index)Returns the location ofindexin terms of the underlying model.abstract intconvertRowIndexToView(int index)Returns the location ofindexin terms of the view.protected voidfireRowSorterChanged(int[] lastRowIndexToModel)Notifies listener that the mapping has changed.protected voidfireSortOrderChanged()Notifies listener that the sort order has changed.abstract MgetModel()Returns the underlying model.abstract intgetModelRowCount()Returns the number of rows in the underlying model.abstract List<? extends RowSorter.SortKey>getSortKeys()Returns the current sort keys.abstract intgetViewRowCount()Returns the number of rows in the view.abstract voidmodelStructureChanged()Invoked when the underlying model structure has completely changed.voidremoveRowSorterListener(RowSorterListener l)Removes aRowSorterListener.abstract voidrowsDeleted(int firstRow, int endRow)Invoked when rows have been deleted from the underlying model in the specified range (inclusive).abstract voidrowsInserted(int firstRow, int endRow)Invoked when rows have been inserted into the underlying model in the specified range (inclusive).abstract voidrowsUpdated(int firstRow, int endRow)Invoked when rows have been changed in the underlying model between the specified range (inclusive).abstract 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.abstract voidsetSortKeys(List<? extends RowSorter.SortKey> keys)Sets the current sort keys.abstract voidtoggleSortOrder(int column)Reverses the sort order of the specified column.
-
-
-
Method Detail
-
getModel
public abstract M getModel()
Returns the underlying model.- Returns:
- the underlying model
-
toggleSortOrder
public abstract void toggleSortOrder(int column)
Reverses the sort order of the specified column. It is up to subclasses to provide the exact behavior when invoked. Typically this will reverse 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.If this results in changing the sort order and sorting, the appropriate
RowSorterListenernotification will be sent.- Parameters:
column- the column to toggle the sort ordering of, in terms of the underlying model- Throws:
IndexOutOfBoundsException- if column is outside the range of the underlying model
-
convertRowIndexToModel
public abstract 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.- Parameters:
index- the row index in terms of the underlying view- Returns:
- row index in terms of the view
- Throws:
IndexOutOfBoundsException- ifindexis outside the range of the view
-
convertRowIndexToView
public abstract 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.- 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- ifindexis outside the range of the model
-
setSortKeys
public abstract void setSortKeys(List<? extends RowSorter.SortKey> keys)
Sets the current sort keys.- Parameters:
keys- the newSortKeys;nullis a shorthand for specifying an empty list, indicating that the view should be unsorted
-
getSortKeys
public abstract List<? extends RowSorter.SortKey> getSortKeys()
Returns the current sort keys. This must return anon-null Listand may return an unmodifiableList. If you need to change the sort keys, make a copy of the returnedList, mutate the copy and invokesetSortKeyswith the new list.- Returns:
- the current sort order
-
getViewRowCount
public abstract int getViewRowCount()
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.- Returns:
- number of rows in the view
- See Also:
getModelRowCount()
-
getModelRowCount
public abstract int getModelRowCount()
Returns the number of rows in the underlying model.- Returns:
- number of rows in the underlying model
- See Also:
getViewRowCount()
-
modelStructureChanged
public abstract void modelStructureChanged()
Invoked when the underlying model structure has completely changed. For example, if the number of columns in aTableModelchanged, this method would be invoked.You normally do not call this method. This method is public to allow view classes to call it.
-
allRowsChanged
public abstract void allRowsChanged()
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.
-
rowsInserted
public abstract 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.
- Parameters:
firstRow- the first rowendRow- the last row- Throws:
IndexOutOfBoundsException- if either argument is invalid, orfirstRow>endRow
-
rowsDeleted
public abstract 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.
- Parameters:
firstRow- the first rowendRow- the last row- Throws:
IndexOutOfBoundsException- if either argument is outside the range of the model before the change, orfirstRow>endRow
-
rowsUpdated
public abstract 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.
- Parameters:
firstRow- the first row, in terms of the underlying modelendRow- the last row, in terms of the underlying model- Throws:
IndexOutOfBoundsException- if either argument is outside the range of the underlying model, orfirstRow>endRow
-
rowsUpdated
public abstract 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.
- Parameters:
firstRow- the first row, in terms of the underlying modelendRow- the last row, in terms of the underlying modelcolumn- 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, orcolumnis outside the range of the underlying model
-
addRowSorterListener
public void addRowSorterListener(RowSorterListener l)
Adds aRowSorterListenerto receive notification about thisRowSorter. If the same listener is added more than once it will receive multiple notifications. Iflisnullnothing is done.- Parameters:
l- theRowSorterListener
-
removeRowSorterListener
public void removeRowSorterListener(RowSorterListener l)
Removes aRowSorterListener. Iflisnullnothing is done.- Parameters:
l- theRowSorterListener
-
fireSortOrderChanged
protected void fireSortOrderChanged()
Notifies listener that the sort order has changed.
-
fireRowSorterChanged
protected void fireRowSorterChanged(int[] lastRowIndexToModel)
Notifies listener that the mapping has changed.- Parameters:
lastRowIndexToModel- the mapping from model indices to view indices prior to the sort, may benull
-
-