- java.lang.Object
-
- javax.swing.RowSorter<M>
-
- javax.swing.DefaultRowSorter<M,Integer>
-
- javax.swing.table.TableRowSorter<M>
-
- Type Parameters:
M- the type of the model, which must be an implementation ofTableModel
public class TableRowSorter<M extends TableModel> extends DefaultRowSorter<M,Integer>
An implementation ofRowSorterthat provides sorting and filtering using aTableModel. The following example shows adding sorting to aJTable:TableModel myModel = createMyTableModel(); JTable table = new JTable(myModel); table.setRowSorter(new TableRowSorter(myModel));
This will do all the wiring such that when the user does the appropriate gesture, such as clicking on the column header, the table will visually sort.JTable's row-based methods andJTable's selection model refer to the view and not the underlying model. Therefore, it is necessary to convert between the two. For example, to get the selection in terms ofmyModelyou need to convert the indices:int[] selection = table.getSelectedRows(); for (int i = 0; i < selection.length; i++) { selection[i] = table.convertRowIndexToModel(selection[i]); }Similarly to select a row inJTablebased on a coordinate from the underlying model do the inverse:table.setRowSelectionInterval(table.convertRowIndexToView(row), table.convertRowIndexToView(row));The previous example assumes you have not enabled filtering. If you have enabled filtering
convertRowIndexToViewwill return -1 for locations that are not visible in the view.TableRowSorterusesComparators for doing comparisons. The following defines how aComparatoris chosen for a column:- If a
Comparatorhas been specified for the column by thesetComparatormethod, use it. - If the column class as returned by
getColumnClassisString, use theComparatorreturned byCollator.getInstance(). - If the column class implements
Comparable, use aComparatorthat invokes thecompareTomethod. - If a
TableStringConverterhas been specified, use it to convert the values toStrings and then use theComparatorreturned byCollator.getInstance(). - Otherwise use the
Comparatorreturned byCollator.getInstance()on the results from callingtoStringon the objects.
In addition to sorting
TableRowSorterprovides the ability to filter. A filter is specified using thesetFiltermethod. The following example will only show rows containing the string "foo":TableModel myModel = createMyTableModel(); TableRowSorter sorter = new TableRowSorter(myModel); sorter.setRowFilter(RowFilter.regexFilter(".*foo.*")); JTable table = new JTable(myModel); table.setRowSorter(sorter);If the underlying model structure changes (the
modelStructureChangedmethod is invoked) the following are reset to their default values:Comparators by column, current sort order, and whether each column is sortable. The default sort order is natural (the same as the model), and columns are sortable by default.TableRowSorterhas one formal type parameter: the type of the model. Passing in a type that corresponds exactly to your model allows you to filter based on your model without casting. Refer to the documentation ofRowFilterfor an example of this.WARNING:
DefaultTableModelreturns a column class ofObject. As such all comparisons will be done usingtoString. This may be unnecessarily expensive. If the column only contains one type of value, such as anInteger, you should overridegetColumnClassand return the appropriateClass. This will dramatically increase the performance of this class.- Since:
- 1.6
- See Also:
JTable,RowFilter,DefaultTableModel,Collator,Comparator
-
-
Nested Class Summary
-
Nested classes/interfaces declared in class javax.swing.DefaultRowSorter
DefaultRowSorter.ModelWrapper<M,I>
-
Nested classes/interfaces declared in class javax.swing.RowSorter
RowSorter.SortKey
-
-
Constructor Summary
Constructors Constructor Description TableRowSorter()Creates aTableRowSorterwith an empty model.TableRowSorter(M model)Creates aTableRowSorterusingmodelas the underlyingTableModel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Comparator<?>getComparator(int column)Returns theComparatorfor the specified column.TableStringConvertergetStringConverter()Returns the object responsible for converting values from the model to strings.voidsetModel(M model)Sets theTableModelto use as the underlying model for thisTableRowSorter.voidsetStringConverter(TableStringConverter stringConverter)Sets the object responsible for converting values from the model to strings.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.DefaultRowSorter
convertRowIndexToModel, convertRowIndexToView, getMaxSortKeys, getModel, getModelWrapper, getRowFilter, getSortKeys, getSortsOnUpdates, isSortable, rowsDeleted, rowsInserted, rowsUpdated, rowsUpdated, setComparator, setMaxSortKeys, setModelWrapper, setRowFilter, setSortable, setSortKeys, setSortsOnUpdates, sort, toggleSortOrder
-
Methods declared in class javax.swing.RowSorter
addRowSorterListener, allRowsChanged, fireRowSorterChanged, fireSortOrderChanged, getModelRowCount, getViewRowCount, modelStructureChanged, removeRowSorterListener
-
-
-
-
Constructor Detail
-
TableRowSorter
public TableRowSorter()
Creates aTableRowSorterwith an empty model.
-
TableRowSorter
public TableRowSorter(M model)
Creates aTableRowSorterusingmodelas the underlyingTableModel.- Parameters:
model- the underlyingTableModelto use,nullis treated as an empty model
-
-
Method Detail
-
setModel
public void setModel(M model)
Sets theTableModelto use as the underlying model for thisTableRowSorter. A value ofnullcan be used to set an empty model.- Parameters:
model- the underlying model to use, ornull
-
setStringConverter
public void setStringConverter(TableStringConverter stringConverter)
Sets the object responsible for converting values from the model to strings. If non-nullthis is used to convert any object values, that do not have a registeredComparator, to strings.- Parameters:
stringConverter- the object responsible for converting values from the model to strings
-
getStringConverter
public TableStringConverter getStringConverter()
Returns the object responsible for converting values from the model to strings.- Returns:
- object responsible for converting values to strings.
-
getComparator
public Comparator<?> getComparator(int column)
Returns theComparatorfor the specified column. If aComparatorhas not been specified using thesetComparatormethod aComparatorwill be returned based on the column class (TableModel.getColumnClass) of the specified column. If the column class isString,Collator.getInstanceis returned. If the column class implementsComparablea privateComparatoris returned that invokes thecompareTomethod. OtherwiseCollator.getInstanceis returned.- Overrides:
getComparatorin classDefaultRowSorter<M extends TableModel,Integer>- Parameters:
column- the column to fetch theComparatorfor, in terms of the underlying model- Returns:
- the
Comparatorfor the specified column - Throws:
IndexOutOfBoundsException- if column is outside the range of the underlying model
-
useToString
protected boolean useToString(int column)
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.- Overrides:
useToStringin classDefaultRowSorter<M extends TableModel,Integer>- Parameters:
column- the index of the column to test, in terms of the underlying model- Returns:
- true if values are to be converted to strings before doing comparisons when sorting
- Throws:
IndexOutOfBoundsException- ifcolumnis not valid
-
-