|
The following content comes from the jdk documentation
[Quote]
javax.swing.RowFilter<M,I>
RowFilter is used to filter items from the model so that these items will not be displayed in the view. For example, a RowFilter associated with a JTable may only allow those rows that contain columns with specified strings. The meaning of the entry depends on the component type. For example, when a filter is associated with JTable, an entry corresponds to a row; when a filter is associated with JTree, an entry corresponds to a node.
The subclass must override the include method to indicate whether the item should be displayed in the view. The Entry parameter can be used to get the value of each column in the entry. The following example shows an include method that only allows entries with one or more values beginning with the string "a".
RowFilter<Object,Object> startsWithAFilter = new RowFilter<Object,Object>() {
public boolean include(Entry<? extends Object,? extends Object> entry) {
for (int i = entry.getValueCount()-1; i >= 0; i--) {
if (entry.getStringValue(i).startsWith("a")) {
// The value starts with "a", include it
return true;
}
}
// None of the columns start with "a"; return false so that this
// entry is not shown
return false;
}
};
RowFilter has two formal type parameters that can be used to create RowFilters for specific models. For example, the following code assumes a specific model that wraps objects of type Person. Only show persons older than 20:
RowFilter<PersonModel,Integer> ageFilter = new RowFilter<PersonModel,Integer>() {
public boolean include(Entry<? extends PersonModel,? extends Integer> entry) {
PersonModel personModel = entry.getModel();
Person person = personModel.getPerson(entry.getIdentifier());
if (person.getAge()> 20) {
// Returning true indicates this row should be shown.
return true;
}
// Age is <= 20, don't show it.
return false;
}
};
PersonModel model = createPersonModel();
TableRowSorter<PersonModel> sorter = new TableRowSorter<PersonModel>(model);
sorter.setRowFilter(ageFilter);
From the following version:
1.6[/Quote] |
|