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

public abstract Class DefaultRowSorter<M, I>

extends RowSorter<M>
Class Inheritance
Known Direct Subclasses
javax.swing.table.TableRowSorter
Type Parameters
<M>
the type of the model
<I>
the type of the identifier passed to the RowFilter
Imports
java.text.Collator, java.util.ArrayList, .Arrays, .Collections, .Comparator, .List

An implementation of 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.

Since
1.6
See Also
javax.swing.table.TableRowSorter, javax.swing.table.DefaultTableModel, java.text.Collator

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
DefaultRowSorter.FilterEntry

RowFilter.Entry implementation that delegates to the ModelWrapper.

protected abstract static class
DefaultRowSorter.ModelWrapper<
the type of the underlying model
M
,
the identifier supplied to the filter
I
>

DefaultRowSorter.ModelWrapper is responsible for providing the data that gets sorted by DefaultRowSorter.

private static class
DefaultRowSorter.Row

Row is used to handle the actual sorting by way of Comparable.

Field Summary

Modifier and TypeField and Description
private RowSorter.SortKey[]
cachedSortKeys

Cached SortKeys for the current sort.

private Comparator<?>[]
comparators

Comparators specified by column.

private RowFilter<? super M, ? super I>
filter

Developer supplied Filter.

private DefaultRowSorter<M, I>.FilterEntry
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<M, I>
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<RowSorter.SortKey>
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.Row[]
viewToModel

View (JTable) -> model.

private static boolean

Constructor Summary

AccessConstructor and Description
public
DefaultRowSorter()

Creates an empty DefaultRowSorter.

Method Summary

Modifier and TypeMethod and Description
private void
public void
allRowsChanged()

Implements abstract javax.swing.RowSorter.allRowsChanged.

Invoked when the contents of the underlying model have completely changed.

private void
cacheSortKeys(List<? extends RowSorter.SortKey> keys)

Caches the sort keys before a sort.

private void
checkAgainstModel(int firstRow, int endRow)

private void
checkColumn(int column)

private int
compare(int model1, int model2)

public int
convertRowIndexToModel(int index)

Implements abstract javax.swing.RowSorter.convertRowIndexToModel.

Returns the location of index in terms of the underlying model.

public int
convertRowIndexToView(int index)

Implements abstract javax.swing.RowSorter.convertRowIndexToView.

Returns the location of index in terms of the view.

private int
private void
createModelToView(int rowCount)

Makes sure the modelToView array is of size rowCount.

private void
createViewToModel(int rowCount)

Resets the viewToModel array to be of size rowCount.

public Comparator<?>

Returns:

the Comparator for the specified column
getComparator
(int
the column to fetch the Comparator for, in terms of the underlying model
column
)

Returns the Comparator for the specified column.

private Comparator<?>
getComparator0(int column)

private RowFilter.Entry<M, I>
getFilterEntry(int modelIndex)

public int

Returns:

the maximum number of sort keys
getMaxSortKeys
()

Returns the maximum number of sort keys.

public final M

Returns:

the underlying model
getModel
()

Implements abstract javax.swing.RowSorter.getModel.

Returns the underlying model.

public int
getModelRowCount()

Implements abstract javax.swing.RowSorter.getModelRowCount.

Returns the number of rows in the underlying model.

protected final DefaultRowSorter.ModelWrapper<M, I>

Returns:

the model wrapper responsible for providing the data that gets sorted and filtered
getModelWrapper
()

Returns the model wrapper providing the data that is being sorted and filtered.

public RowFilter<? super M, ? super I>

Returns:

the filter
getRowFilter
()

Returns the filter that determines which rows, if any, should be hidden from view.

public List<? extends RowSorter.SortKey>

Returns:

the current sort order
getSortKeys
()

Implements abstract javax.swing.RowSorter.getSortKeys.

Returns the current sort keys.

public boolean

Returns:

whether or not to sort when the model is updated
getSortsOnUpdates
()

Returns true if a sort should happen when the underlying model is updated; otherwise, returns false.

public int
getViewRowCount()

Implements abstract javax.swing.RowSorter.getViewRowCount.

Returns the number of rows in the view.

private int[]
private boolean
include(int row)

Returns true if the specified row should be included.

private void
initializeFilteredMapping()

Resets the viewToModel and modelToView mappings based on the current Filter.

private void
insertInOrder(List<DefaultRowSorter.Row>
the Rows to add, sorted
toAdd
,
DefaultRowSorter.Row[]
the array to insert the items into
current
)

Insets new set of entries.

public boolean

Returns:

true if the column is sortable
isSortable
(int
the column to check sorting for, in terms of the underlying model
column
)

Returns true if the specified column is sortable; otherwise, false.

private boolean
isTransformed()

Whether not we are filtering/sorting.

private boolean
public void
modelStructureChanged()

Implements abstract javax.swing.RowSorter.modelStructureChanged.

Invoked when the underlying model structure has completely changed.

public void
rowsDeleted(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsDeleted.

Invoked when rows have been deleted from the underlying model in the specified range (inclusive).

private void
rowsDeleted0(int firstRow, int lastRow)

public void
rowsInserted(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsInserted.

Invoked when rows have been inserted into the underlying model in the specified range (inclusive).

private void
rowsInserted0(int firstRow, int lastRow)

public void
rowsUpdated(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsUpdated.

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.swing.RowSorter.rowsUpdated.

Invoked when the column in the rows have been updated in the underlying model between the specified range.

private void
rowsUpdated0(int firstRow, int lastRow)

public void
setComparator(int
the index of the column the Comparator is to be used for, in terms of the underlying model
column
,
Comparator<?>
the Comparator to use
comparator
)

Sets the Comparator to use when sorting the specified column.

public void
setMaxSortKeys(int
the maximum number of sort keys
max
)

Sets the maximum number of sort keys.

private void
setModelToViewFromViewToModel(boolean unsetFirst)

Refreshes the modelToView mapping from that of viewToModel.

protected final void
setModelWrapper(DefaultRowSorter.ModelWrapper<M, I>
the model wrapper responsible for providing the data that gets sorted and filtered
modelWrapper
)

Sets 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.SortKey>
the new SortKeys; null is a shorthand for specifying an empty list, indicating that the view should be unsorted
sortKeys
)

Implements abstract javax.swing.RowSorter.setSortKeys.

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 (rowsUpdated is invoked).

private boolean
shouldOptimizeChange(int firstRow, int lastRow)

Returns true if we should try and optimize the processing of the TableModelEvent.

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
sortExistingData()

Sorts the existing filtered data.

private RowSorter.SortKey
public void
toggleSortOrder(int
index of the column to make the primary sorted column, in terms of the underlying model
column
)

Implements abstract javax.swing.RowSorter.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.

private void
updateUseToString()

Updates the useToString mapping before a sort.

protected boolean

Returns:

true if values are to be converted to strings before doing comparisons when sorting
useToString
(int
the 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.

Inherited from javax.swing.RowSorter:
addRowSorterListenerfireRowSorterChangedfireRowSorterChangedfireSortOrderChangedremoveRowSorterListener

Field Detail

cachedSortKeysback to summary
private RowSorter.SortKey[] cachedSortKeys

Cached SortKeys for the current sort.

comparatorsback to summary
private Comparator<?>[] comparators

Comparators specified by column.

filterback to summary
private RowFilter<? super M, ? super I> filter

Developer supplied Filter.

filterEntryback to summary
private DefaultRowSorter<M, I>.FilterEntry filterEntry

Value passed to the filter. The same instance is passed to the filter for different rows.

isSortableback to summary
private boolean[] isSortable

Whether or not the specified column is sortable, by column.

maxSortKeysback to summary
private int maxSortKeys

Maximum number of sort keys.

modelRowCountback 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).

modelToViewback to summary
private int[] modelToView

model -> view (JTable)

modelWrapperback to summary
private DefaultRowSorter.ModelWrapper<M, I> modelWrapper

Provides access to the data we're sorting/filtering.

sortComparatorsback to summary
private Comparator<?>[] sortComparators

Cached comparators for the current sort

sortedback to summary
private boolean sorted

Indicates the contents are sorted. This is used if getSortsOnUpdates is false and an update event is received.

sortKeysback to summary
private List<RowSorter.SortKey> sortKeys

The sort keys.

sortsOnUpdatesback to summary
private boolean sortsOnUpdates

Whether or not we resort on TableModelEvent.UPDATEs.

useToStringback to summary
private boolean[] useToString

Whether or not to use getStringValueAt. This is indexed by column.

viewToModelback to summary
private DefaultRowSorter.Row[] viewToModel

View (JTable) -> model.

warning8160087back to summary
private static boolean warning8160087

Constructor Detail

DefaultRowSorterback to summary
public DefaultRowSorter()

Creates an empty DefaultRowSorter.

Method Detail

allChangedback to summary
private void allChanged()
allRowsChangedback to summary
public void allRowsChanged()

Implements abstract javax.swing.RowSorter.allRowsChanged.

Doc from javax.swing.RowSorter.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.

cacheSortKeysback to summary
private void cacheSortKeys(List<? extends RowSorter.SortKey> keys)

Caches the sort keys before a sort.

checkAgainstModelback to summary
private void checkAgainstModel(int firstRow, int endRow)
checkColumnback to summary
private void checkColumn(int column)
compareback to summary
private int compare(int model1, int model2)
Annotations
@SuppressWarnings:unchecked
convertRowIndexToModelback to summary
public int convertRowIndexToModel(int index)

Implements abstract javax.swing.RowSorter.convertRowIndexToModel.

Doc from javax.swing.RowSorter.convertRowIndexToModel.

Returns the location of index in terms of the underlying model. That is, for the row index in the coordinates of the view this returns the row index in terms of the underlying model.

Exceptions
IndexOutOfBoundsException:
if index is outside the range of the view
convertRowIndexToViewback to summary
public int convertRowIndexToView(int index)

Implements abstract javax.swing.RowSorter.convertRowIndexToView.

Doc from javax.swing.RowSorter.convertRowIndexToView.

Returns the location of index in terms of the view. That is, for the row index in the coordinates of the underlying model this returns the row index in terms of the view.

Exceptions
IndexOutOfBoundsException:
if index is outside the range of the model
convertUnsortedUnfilteredback to summary
private int convertUnsortedUnfiltered(int index)
createModelToViewback to summary
private void createModelToView(int rowCount)

Makes sure the modelToView array is of size rowCount.

createViewToModelback to summary
private void createViewToModel(int rowCount)

Resets the viewToModel array to be of size rowCount.

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

Returns the Comparator for the specified column. This will return null if a Comparator has not been specified for the column.

Parameters
column:int

the column to fetch the Comparator for, in terms of the underlying model

Returns:Comparator<?>

the Comparator for the specified column

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the underlying model
getComparator0back to summary
private Comparator<?> getComparator0(int column)
getFilterEntryback to summary
private RowFilter.Entry<M, I> getFilterEntry(int modelIndex)
getMaxSortKeysback to summary
public int getMaxSortKeys()

Returns the maximum number of sort keys.

Returns:int

the maximum number of sort keys

getModelback to summary
public final M getModel()

Implements abstract javax.swing.RowSorter.getModel.

Returns the underlying model.

Returns:M

the underlying model

getModelRowCountback to summary
public int getModelRowCount()

Implements abstract javax.swing.RowSorter.getModelRowCount.

Doc from javax.swing.RowSorter.getModelRowCount.

Returns the number of rows in the underlying model.

getModelWrapperback to summary
protected final DefaultRowSorter.ModelWrapper<M, I> getModelWrapper()

Returns the model wrapper providing the data that is being sorted and filtered.

Returns:DefaultRowSorter.ModelWrapper<M, I>

the model wrapper responsible for providing the data that gets sorted and filtered

getRowFilterback to summary
public RowFilter<? super M, ? super I> getRowFilter()

Returns the filter that determines which rows, if any, should be hidden from view.

Returns:RowFilter<? super M, ? super I>

the filter

getSortKeysback to summary
public List<? extends RowSorter.SortKey> getSortKeys()

Implements abstract javax.swing.RowSorter.getSortKeys.

Returns the current sort keys. This returns an unmodifiable non-null List. If you need to change the sort keys, make a copy of the returned List, mutate the copy and invoke setSortKeys with the new list.

Returns:List<? extends RowSorter.SortKey>

the current sort order

getSortsOnUpdatesback to summary
public boolean getSortsOnUpdates()

Returns true if a sort should happen when the underlying model is updated; otherwise, returns false.

Returns:boolean

whether or not to sort when the model is updated

getViewRowCountback to summary
public int getViewRowCount()

Implements abstract javax.swing.RowSorter.getViewRowCount.

Doc from javax.swing.RowSorter.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.

getViewToModelAsIntsback to summary
private int[] getViewToModelAsInts(DefaultRowSorter.Row[] viewToModel)
includeback to summary
private boolean include(int row)

Returns true if the specified row should be included.

initializeFilteredMappingback to summary
private void initializeFilteredMapping()

Resets the viewToModel and modelToView mappings based on the current Filter.

insertInOrderback to summary
private void insertInOrder(List<DefaultRowSorter.Row> toAdd, DefaultRowSorter.Row[] current)

Insets new set of entries.

Parameters
toAdd:List<DefaultRowSorter.Row>

the Rows to add, sorted

current:DefaultRowSorter.Row[]

the array to insert the items into

isSortableback to summary
public boolean isSortable(int column)

Returns true if the specified column is sortable; otherwise, false.

Parameters
column:int

the column to check sorting for, in terms of the underlying model

Returns:boolean

true if the column is sortable

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the underlying model
isTransformedback to summary
private boolean isTransformed()

Whether not we are filtering/sorting.

isUnsortedback to summary
private boolean isUnsorted()
modelStructureChangedback to summary
public void modelStructureChanged()

Implements abstract javax.swing.RowSorter.modelStructureChanged.

Doc from javax.swing.RowSorter.modelStructureChanged.

Invoked when the underlying model structure has completely changed. For example, if the number of columns in a TableModel changed, this method would be invoked.

You normally do not call this method. This method is public to allow view classes to call it.

rowsDeletedback to summary
public void rowsDeleted(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsDeleted.

Doc from javax.swing.RowSorter.rowsDeleted.

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.

Exceptions
IndexOutOfBoundsException:
if either argument is outside the range of the model before the change, or firstRow > endRow
rowsDeleted0back to summary
private void rowsDeleted0(int firstRow, int lastRow)
rowsInsertedback to summary
public void rowsInserted(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsInserted.

Doc from javax.swing.RowSorter.rowsInserted.

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.

Exceptions
IndexOutOfBoundsException:
if either argument is invalid, or firstRow > endRow
rowsInserted0back to summary
private void rowsInserted0(int firstRow, int lastRow)
rowsUpdatedback to summary
public void rowsUpdated(int firstRow, int endRow)

Implements abstract javax.swing.RowSorter.rowsUpdated.

Doc from javax.swing.RowSorter.rowsUpdated.

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.

Exceptions
IndexOutOfBoundsException:
if either argument is outside the range of the underlying model, or firstRow > endRow
rowsUpdatedback to summary
public void rowsUpdated(int firstRow, int endRow, int column)

Implements abstract javax.swing.RowSorter.rowsUpdated.

Doc from javax.swing.RowSorter.rowsUpdated.

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.

Exceptions
IndexOutOfBoundsException:
if either argument is outside the range of the underlying model after the change, firstRow > endRow, or column is outside the range of the underlying model
rowsUpdated0back to summary
private void rowsUpdated0(int firstRow, int lastRow)
setComparatorback to summary
public void setComparator(int column, Comparator<?> comparator)

Sets the Comparator to 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 invoke sort.

Parameters
column:int

the index of the column the Comparator is to be used for, in terms of the underlying model

comparator:Comparator<?>

the Comparator to use

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the underlying model
setMaxSortKeysback 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 setMaxSortKeys(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 with setMaxSortKeys, 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 invoking setSortKeys directly and they will all be honored. However if toggleSortOrder is subsequently invoked the maximum number of sort keys will be enforced. The default value is 3.

Parameters
max:int

the maximum number of sort keys

Exceptions
IllegalArgumentException:
if max < 1
setModelToViewFromViewToModelback to summary
private void setModelToViewFromViewToModel(boolean unsetFirst)

Refreshes the modelToView mapping from that of viewToModel. If unsetFirst is true, all indices in modelToView are first set to -1.

setModelWrapperback to summary
protected final void setModelWrapper(DefaultRowSorter.ModelWrapper<M, I> modelWrapper)

Sets the model wrapper providing the data that is being sorted and filtered.

Parameters
modelWrapper:DefaultRowSorter.ModelWrapper<M, I>

the model wrapper responsible for providing the data that gets sorted and filtered

Exceptions
IllegalArgumentException:
if modelWrapper is null
setRowFilterback 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 null indicates all values from the model should be included.

RowFilter's include method is passed an Entry that wraps the underlying model. The number of columns in the Entry corresponds to the number of columns in the ModelWrapper. The identifier comes from the ModelWrapper as well.

This method triggers a sort.

Parameters
filter:RowFilter<? super M, ? super I>

the filter used to determine what entries should be included

setSortableback 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 toggleSortOrder is 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:int

the column to enable or disable sorting on, in terms of the underlying model

sortable:boolean

whether or not the specified column is sortable

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the model
See Also
toggleSortOrder, setSortKeys
setSortKeysback to summary
public void setSortKeys(List<? extends RowSorter.SortKey> sortKeys)

Implements abstract javax.swing.RowSorter.setSortKeys.

Sets the sort keys. This creates a copy of the supplied List; subsequent changes to the supplied List do not effect this DefaultRowSorter. If the sort keys have changed this triggers a sort.

Parameters
sortKeys:List<? extends RowSorter.SortKey>

the new SortKeys; null is a shorthand for specifying an empty list, indicating that the view should be unsorted

Exceptions
IllegalArgumentException:
if any of the values in sortKeys are null or have a column index outside the range of the model
setSortsOnUpdatesback to summary
public void setSortsOnUpdates(boolean sortsOnUpdates)

If true, specifies that a sort should happen when the underlying model is updated (rowsUpdated is 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:boolean

whether or not to sort on update events

shouldOptimizeChangeback to summary
private boolean shouldOptimizeChange(int firstRow, int lastRow)

Returns true if we should try and optimize the processing of the TableModelEvent. If this returns false, assume the event was dealt with and no further processing needs to happen.

sortback 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 sortKeys list indicates that the view should unsorted, the same as the model.

See Also
setRowFilter, setSortKeys
sortExistingDataback to summary
private void sortExistingData()

Sorts the existing filtered data. This should only be used if the filter hasn't changed.

toggleback to summary
private RowSorter.SortKey toggle(RowSorter.SortKey key)
toggleSortOrderback to summary
public void toggleSortOrder(int column)

Implements abstract javax.swing.RowSorter.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. If the specified column is not sortable, this method has no effect.

Parameters
column:int

index of the column to make the primary sorted column, in terms of the underlying model

Exceptions
IndexOutOfBoundsException:
if column is outside the range of the underlying model
See Also
setSortable(int, boolean), setMaxSortKeys(int)
updateUseToStringback to summary
private void updateUseToString()

Updates the useToString mapping before a sort.

useToStringback 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 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.

Parameters
column:int

the index of the column to test, in terms of the underlying model

Returns:boolean

true if values are to be converted to strings before doing comparisons when sorting

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

private Class DefaultRowSorter.FilterEntry

extends RowFilter.Entry<M, I>
Class Inheritance

RowFilter.Entry implementation that delegates to the ModelWrapper. getFilterEntry(int) creates the single instance of this that is passed to the Filter. Only call getFilterEntry(int) to get the instance.

Field Summary

Modifier and TypeField and Description
pack-priv int
modelIndex

The index into the model, set in getFilterEntry

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public I
getIdentifier()

Implements abstract javax.swing.RowFilter.Entry.getIdentifier.

Returns the identifier (in the model) of the entry.

public M
getModel()

Implements abstract javax.swing.RowFilter.Entry.getModel.

Returns the underlying model.

public String
getStringValue(int index)

Overrides javax.swing.RowFilter.Entry.getStringValue.

Returns the string value at the specified index.

public Object
getValue(int index)

Implements abstract javax.swing.RowFilter.Entry.getValue.

Returns the value at the specified index.

public int
getValueCount()

Implements abstract javax.swing.RowFilter.Entry.getValueCount.

Returns the number of values in the entry.

Field Detail

modelIndexback to summary
pack-priv int modelIndex

The index into the model, set in getFilterEntry

Constructor Detail

FilterEntryback to summary
private FilterEntry()

Method Detail

getIdentifierback to summary
public I getIdentifier()

Implements abstract javax.swing.RowFilter.Entry.getIdentifier.

Doc from javax.swing.RowFilter.Entry.getIdentifier.

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 Integer.

getModelback to summary
public M getModel()

Implements abstract javax.swing.RowFilter.Entry.getModel.

Doc from javax.swing.RowFilter.Entry.getModel.

Returns the underlying model.

getStringValueback to summary
public String getStringValue(int index)

Overrides javax.swing.RowFilter.Entry.getStringValue.

Doc from javax.swing.RowFilter.Entry.getStringValue.

Returns the string value at the specified index. If filtering is being done based on String values this method is preferred to that of getValue as getValue(index).toString() may return a different result than getStringValue(index).

This implementation calls getValue(index).toString() after checking for null. Subclasses that provide different string conversion should override this method if necessary.

getValueback to summary
public Object getValue(int index)

Implements abstract javax.swing.RowFilter.Entry.getValue.

Doc from javax.swing.RowFilter.Entry.getValue.

Returns the value at the specified index. This may return null. When used with a table, index corresponds to the column number in the model.

getValueCountback to summary
public int getValueCount()

Implements abstract javax.swing.RowFilter.Entry.getValueCount.

Doc from javax.swing.RowFilter.Entry.getValueCount.

Returns the number of values in the entry. For example, when used with a table this corresponds to the number of columns.

javax.swing back to summary

protected abstract Class DefaultRowSorter.ModelWrapper<M, I>

extends Object
Class Inheritance
Known Direct Subclasses
javax.swing.table.TableRowSorter.TableRowSorterModelWrapper, sun.swing.FilePane.DetailsTableRowSorter.SorterModelWrapper
Type Parameters
<M>
the type of the underlying model
<I>
the identifier supplied to the filter

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.

Since
1.6
See Also
RowFilter, RowFilter.Entry

Constructor Summary

AccessConstructor and Description
protected
ModelWrapper()

Creates a new ModelWrapper.

Method Summary

Modifier and TypeMethod and Description
public abstract int

Returns:

the number of columns in the model
getColumnCount
()

Returns the number of columns in the model.

public abstract I

Returns:

the identifier
getIdentifier
(int
the row to return the identifier for, in terms of the underlying model
row
)

Returns the identifier for the specified row.

public abstract M

Returns:

the underlying model
getModel
()

Returns the underlying model that this Model is wrapping.

public abstract int

Returns:

the number of rows in the model
getRowCount
()

Returns the number of rows in the model.

public String

Returns:

the value at the specified index as a String
getStringValueAt
(int
the row index
row
,
int
the column index
column
)

Returns the value as a String at the specified index.

public abstract Object

Returns:

the value at the specified index
getValueAt
(int
the row index
row
,
int
the column index
column
)

Returns the value at the specified index.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ModelWrapperback to summary
protected ModelWrapper()

Creates a new ModelWrapper.

Method Detail

getColumnCountback to summary
public abstract int getColumnCount()

Returns the number of columns in the model.

Returns:int

the number of columns in the model

getIdentifierback 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 RowFilter.Entry that is passed to the RowFilter.

Parameters
row:int

the row to return the identifier for, in terms of the underlying model

Returns:I

the identifier

See Also
RowFilter.Entry#getIdentifier
getModelback to summary
public abstract M getModel()

Returns the underlying model that this Model is wrapping.

Returns:M

the underlying model

getRowCountback to summary
public abstract int getRowCount()

Returns the number of rows in the model.

Returns:int

the number of rows in the model

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

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.

Parameters
row:int

the row index

column:int

the column index

Returns:String

the value at the specified index as a String

Exceptions
IndexOutOfBoundsException:
if the indices are outside the range of the model
getValueAtback to summary
public abstract Object getValueAt(int row, int column)

Returns the value at the specified index.

Parameters
row:int

the row index

column:int

the column index

Returns:Object

the value at the specified index

Exceptions
IndexOutOfBoundsException:
if the indices are outside the range of the model
javax.swing back to summary

private Class DefaultRowSorter.Row

extends Object
implements Comparable<DefaultRowSorter.Row>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable

Row is used to handle the actual sorting by way of Comparable. It will use the sortKeys to do the actual comparison.

Field Summary

Modifier and TypeField and Description
pack-priv int
private DefaultRowSorter<?, ?>

Constructor Summary

AccessConstructor and Description
public
Row(DefaultRowSorter<?, ?> sorter, int index)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(DefaultRowSorter.Row o)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

modelIndexback to summary
pack-priv int modelIndex
sorterback to summary
private DefaultRowSorter<?, ?> sorter

Constructor Detail

Rowback to summary
public Row(DefaultRowSorter<?, ?> sorter, int index)

Method Detail

compareToback to summary
public int compareTo(DefaultRowSorter.Row o)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

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 signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

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

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