模块 java.desktop

类 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 可能只允许包含具有特定字符串的列的行。 entry 的含义取决于组件类型。例如,当过滤器与 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
参见:
  • 内部类总结

    内部类
    修饰符和类型
    描述
    static enum 
    一些默认 RowFilter 支持的可能比较值的枚举。
    static class 
    Entry 对象被传递给 RowFilter 的实例,允许过滤器获取条目数据的值,从而确定是否应显示该条目。
  • 构造方法总结

    构造方法
    修饰符
    构造方法
    描述
    protected
    子类调用的构造方法。
  • 方法总结

    修饰符和类型
    方法
    描述
    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

      protected 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

      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      regex - 要过滤的正则表达式
      indices - 要检查的值的索引。如果未提供,则评估所有值
      返回:
      RowFilter 执行指定的标准
      抛出:
      NullPointerException - 如果 regexnull
      IllegalArgumentException - 如果 indices 中的任何一个 < 0
      PatternSyntaxException - 如果 regex 不是有效的正则表达式。
      参见:
    • 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());
       
      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      type - 要执行的比较类型
      date - 要比较的日期
      indices - 要检查的值的索引。如果未提供,则评估所有值
      返回:
      RowFilter 执行指定的标准
      抛出:
      NullPointerException - 如果 datenull
      IllegalArgumentException - 如果 indices 中的任何一个 < 0 或 typenull
      参见:
    • numberFilter

      public static <M, I> RowFilter <M,I> numberFilter(RowFilter.ComparisonType  type, Number  number, int... indices)
      返回一个 RowFilter,其中包括具有至少一个 Number 值满足指定条件的条目。例如,以下过滤器将仅包含至少一个数值等于 10 的条目:
        RowFilter.numberFilter(ComparisonType.EQUAL, 10);
       
      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      type - 要执行的比较类型
      number - 要比较的 Number
      indices - 要检查的值的索引。如果未提供,则评估所有值
      返回:
      RowFilter 执行指定的标准
      抛出:
      IllegalArgumentException - 如果 indices 中的任何一个 < 0,则 typenullnumbernull
    • 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);
       
      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      filters - 要测试的 RowFilter s
      返回:
      RowFilter 执行指定的标准
      抛出:
      IllegalArgumentException - 如果任何过滤器是 null
      NullPointerException - 如果 filters 为空
      参见:
    • 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);
       
      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      filters - 要测试的 RowFilter s
      返回:
      RowFilter 执行指定的标准
      抛出:
      IllegalArgumentException - 如果任何过滤器是 null
      NullPointerException - 如果 filters 为空
      参见:
    • notFilter

      public static <M, I> RowFilter <M,I> notFilter(RowFilter <M,I> filter)
      如果提供的过滤器不包含条目,则返回包含条目的 RowFilter
      类型参数:
      M - RowFilter 适用的模型类型
      I - 传递给 RowFilter 的标识符的类型
      参数:
      filter - RowFilter 否定
      返回:
      RowFilter 执行指定的标准
      抛出:
      IllegalArgumentException - 如果 filternull
    • include

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

      entry 参数仅在调用期间有效。在调用返回后使用 entry 会导致未定义的行为。

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