- 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 ofRowSorter
that 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 ofmyModel
you 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 inJTable
based 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
convertRowIndexToView
will return -1 for locations that are not visible in the view.TableRowSorter
usesComparator
s for doing comparisons. The following defines how aComparator
is chosen for a column:- If a
Comparator
has been specified for the column by thesetComparator
method, use it. - If the column class as returned by
getColumnClass
isString
, use theComparator
returned byCollator.getInstance()
. - If the column class implements
Comparable
, use aComparator
that invokes thecompareTo
method. - If a
TableStringConverter
has been specified, use it to convert the values toString
s and then use theComparator
returned byCollator.getInstance()
. - Otherwise use the
Comparator
returned byCollator.getInstance()
on the results from callingtoString
on the objects.
In addition to sorting
TableRowSorter
provides the ability to filter. A filter is specified using thesetFilter
method. 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
modelStructureChanged
method is invoked) the following are reset to their default values:Comparator
s 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.TableRowSorter
has 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 ofRowFilter
for an example of this.WARNING:
DefaultTableModel
returns 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 overridegetColumnClass
and 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 inherited from class javax.swing.DefaultRowSorter
DefaultRowSorter.ModelWrapper<M,I>
-
Nested classes/interfaces inherited from class javax.swing.RowSorter
RowSorter.SortKey
-
-
Constructor Summary
Constructors Constructor and Description TableRowSorter()
Creates aTableRowSorter
with an empty model.TableRowSorter(M model)
Creates aTableRowSorter
usingmodel
as the underlyingTableModel
.
-
Method Summary
Methods Modifier and Type Method and Description Comparator<?>
getComparator(int column)
Returns theComparator
for the specified column.TableStringConverter
getStringConverter()
Returns the object responsible for converting values from the model to strings.void
setModel(M model)
Sets theTableModel
to use as the underlying model for thisTableRowSorter
.void
setStringConverter(TableStringConverter stringConverter)
Sets the object responsible for converting values from the model to strings.protected boolean
useToString(int column)
Returns whether or not to convert the value to a string before doing comparisons when sorting.-
Methods inherited from class javax.swing.DefaultRowSorter
allRowsChanged, convertRowIndexToModel, convertRowIndexToView, getMaxSortKeys, getModel, getModelRowCount, getModelWrapper, getRowFilter, getSortKeys, getSortsOnUpdates, getViewRowCount, isSortable, modelStructureChanged, rowsDeleted, rowsInserted, rowsUpdated, rowsUpdated, setComparator, setMaxSortKeys, setModelWrapper, setRowFilter, setSortable, setSortKeys, setSortsOnUpdates, sort, toggleSortOrder
-
Methods inherited from class javax.swing.RowSorter
addRowSorterListener, fireRowSorterChanged, fireSortOrderChanged, removeRowSorterListener
-
-
-
-
Constructor Detail
-
TableRowSorter
public TableRowSorter()
Creates aTableRowSorter
with an empty model.
-
TableRowSorter
public TableRowSorter(M model)
Creates aTableRowSorter
usingmodel
as the underlyingTableModel
.- Parameters:
model
- the underlyingTableModel
to use,null
is treated as an empty model
-
-
Method Detail
-
setModel
public void setModel(M model)
Sets theTableModel
to use as the underlying model for thisTableRowSorter
. A value ofnull
can 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-null
this 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 theComparator
for the specified column. If aComparator
has not been specified using thesetComparator
method aComparator
will be returned based on the column class (TableModel.getColumnClass
) of the specified column. If the column class isString
,Collator.getInstance
is returned. If the column class implementsComparable
a privateComparator
is returned that invokes thecompareTo
method. OtherwiseCollator.getInstance
is returned.- Overrides:
getComparator
in classDefaultRowSorter<M extends TableModel,Integer>
- Parameters:
column
- the column to fetch theComparator
for, in terms of the underlying model- Returns:
- the
Comparator
for 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.getStringValueAt
will be used, otherwiseModelWrapper.getValueAt
will be used. It is up to subclasses, such asTableRowSorter
, to honor this value in theirModelWrapper
implementation.- Overrides:
useToString
in classDefaultRowSorter<M extends TableModel,Integer>
- Parameters:
column
- the index of the column to test, in terms of the underlying model- Throws:
IndexOutOfBoundsException
- ifcolumn
is not valid
-
-
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 11/06/2005 gemaakt, de laatste keer de 04/03/2020 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/java-api-rf-javax/swing/table/TableRowSorter.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.