JavaTM Platform
Standard Ed. 6

javax.swing
类 RowFilter<M,I>

java.lang.Object
  继承者 javax.swing.RowFilter<M,I>
类型参数:
M - 模型的类型;例如 PersonModel
I - 标识符的类型;使用 TableRowSorter 时,此类型将是 Integer

public abstract class RowFilter<M,I>
extends Object

RowFilter 用于从模型中过滤条目,使得这些条目不会在视图中显示。例如,一个与 JTable 关联的 RowFilter 可能只允许包含带指定字符串的列的那些行。条目 的含义取决于组件类型。例如,当过滤器与 JTable 关联时,一个条目对应于一行;当过滤器与 JTree 关联时,一个条目对应于一个节点。

子类必须重写 include 方法指示是否应该在视图中显示该条目。Entry 参数可用于获取该条目中每一列的值。下例显示了一个 include 方法,该方法只允许包含以字符串“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 有两个形式类型参数,可用来为特定模型创建 RowFilter。例如,以下代码假定一个包装 Person 类型对象的特定模型。只显示年龄大于 20 的 Person
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);
 

从以下版本开始:
1.6
另请参见:
TableRowSorter

嵌套类摘要
static class RowFilter.ComparisonType
          由某些默认 RowFilter 支持的可能比较值的枚举。
static class RowFilter.Entry<M,I>
          一个传递给 RowFilter 实例的 Entry 对象,允许过滤器获取该条目的数据的值,以确定是否应该显示该条目。
 
构造方法摘要
RowFilter()
           
 
方法摘要
static
<M,I> RowFilter<M,I>
andFilter(Iterable<? extends RowFilter<? super M,? super I>> filters)
          返回一个 RowFilter,它包含所有提供的过滤器所包含的条目。
static
<M,I> RowFilter<M,I>
dateFilter(RowFilter.ComparisonType type, Date date, int... indices)
          返回一个 RowFilter,它包含至少具有一个符合指定标准的 Date 值的条目。
abstract  boolean include(RowFilter.Entry<? extends M,? extends I> entry)
          如果应该显示指定的条目,则返回 true;如果应该隐藏该条目,则返回 false。
static
<M,I> RowFilter<M,I>
notFilter(RowFilter<M,I> filter)
          返回一个 RowFilter,它包含提供的过滤器不包含的条目。
static
<M,I> RowFilter<M,I>
numberFilter(RowFilter.ComparisonType type, Number number, int... indices)
          返回一个 RowFilter,它包含至少具有一个符合指定标准的 Number 值的条目。
static
<M,I> RowFilter<M,I>
orFilter(Iterable<? extends RowFilter<? super M,? super I>> filters)
          返回一个 RowFilter,它包含所有提供的过滤器所包含的条目。
static
<M,I> RowFilter<M,I>
regexFilter(String regex, int... indices)
          返回一个 RowFilter,它使用正则表达式确定要包含哪些条目。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

RowFilter

public RowFilter()
方法详细信息

regexFilter

public static <M,I> RowFilter<M,I> regexFilter(String regex,
                                               int... indices)
返回一个 RowFilter,它使用正则表达式确定要包含哪些条目。只包含至少有一个匹配值的条目。例如,以下代码创建了一个 RowFilter,它包含其值至少有一个以“a”开头的条目。
RowFilter.regexFilter("^a");
 

返回的过滤器使用 Matcher.find() 对包含进行测试。若要测试完全匹配,可分别使用字符 '^' 和 '$' 来匹配该字符串的开头和结尾。例如,“^foo$”只包含其字符串完全为“foo”的行,而不是“food”之类。有关受支持的正则表达式结构的完整描述,请参阅 Pattern

参数:
regex - 在其上进行过滤的正则表达式
indices - 要检查的值的索引如果没有提供,则计算所有的值
返回:
一个实现指定标准的 RowFilter
抛出:
NullPointerException - 如果 regexnull
IllegalArgumentException - 如果任一 indices 值小于 0
PatternSyntaxException - 如果 regex 不是有效的正则表达式
另请参见:
Pattern

dateFilter

public static <M,I> RowFilter<M,I> dateFilter(RowFilter.ComparisonType type,
                                              Date date,
                                              int... indices)
返回一个 RowFilter,它包含至少具有一个符合指定标准的 Date 值的条目。例如,下面的 RowFilter 只包含至少具有一个当前日期之后的日期值的条目:
RowFilter.dateFilter(ComparisonType.AFTER, new Date());
 

参数:
type - 要执行的比较类型
date - 要比较的日期
indices - 要检查的值的索引。如果没有提供,则计算所有的值
返回:
一个实现指定标准的 RowFilter
抛出:
NullPointerException - 如果 datenull
IllegalArgumentException - 如果任一 indices 值小于 0 或 typenull
另请参见:
Calendar, Date

numberFilter

public static <M,I> RowFilter<M,I> numberFilter(RowFilter.ComparisonType type,
                                                Number number,
                                                int... indices)
返回一个 RowFilter,它包含至少具有一个符合指定标准的 Number 值的条目。例如,下面的过滤器将只包含至少具有一个等于 10 的数值的条目:
RowFilter.numberFilter(ComparisonType.EQUAL, 10);
 

参数:
type - 要执行的比较类型
indices - 要检查的值的索引。如果没有提供,则计算所有值
返回:
一个实现指定标准的 RowFilter
抛出:
IllegalArgumentException - 如果任一 indices 值小于 0,typenull 或者 numbernull

orFilter

public static <M,I> RowFilter<M,I> orFilter(Iterable<? extends RowFilter<? super M,? super I>> filters)
返回一个 RowFilter,它包含所有提供的过滤器所包含的条目。

下例创建了一个 RowFilter,它将包括所有包含字符串“foo”或字符串“bar”的条目:

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("foo"));
filters.add(RowFilter.regexFilter("bar"));
RowFilter<Object,Object> fooBarFilter = RowFilter.orFilter(filters);
 

参数:
filters - 要测试的 RowFilter
返回:
一个实现指定标准的 RowFilter
抛出:
IllegalArgumentException - 如果任一过滤器为 null
NullPointerException - 如果 filters 为 null
另请参见:
Arrays.asList(T...)

andFilter

public static <M,I> RowFilter<M,I> andFilter(Iterable<? extends RowFilter<? super M,? super I>> filters)
返回一个 RowFilter,它包含所有提供的过滤器所包含的条目。

下例创建了一个 RowFilter,它将包括所有包含字符串“foo”和字符串“bar”的条目:

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(RowFilter.regexFilter("foo"));
filters.add(RowFilter.regexFilter("bar"));
RowFilter<Object,Object> fooBarFilter = RowFilter.andFilter(filters);
 

参数:
filters - 要测试的 RowFilter
返回:
一个实现指定标准的 RowFilter
抛出:
IllegalArgumentException - 如果任一过滤器为 null
NullPointerException - 如果 filters 为 null
另请参见:
Arrays.asList(T...)

notFilter

public static <M,I> RowFilter<M,I> notFilter(RowFilter<M,I> filter)
返回一个 RowFilter,它包含提供的过滤器不包含的条目。

参数:
filter - 要求反 (negate) 的 RowFilter
返回:
一个实现指定标准的 RowFilter
抛出:
IllegalArgumentException - 如果 filternull

include

public abstract boolean include(RowFilter.Entry<? extends M,? extends I> entry)
如果应该显示指定的条目,则返回 true;如果应该隐藏该条目,则返回 false。

entry 参数只在调用期间有效。在调用返回之后使用 entry 将导致不确定的行为。

参数:
entry - 一个非 null 对象,它包装取自模型的底层对象
返回:
如果应该显示该条目,则返回 true

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策