You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Java GlazedList实现表格列独立过滤功能?

嘿,我来帮你搞定用GlazedList实现多列独立文本框过滤表格的需求!这事儿我之前折腾过好几次,思路其实挺清晰的,咱们一步步来:

用GlazedList实现多列独立文本框过滤表格

1. 先搞定依赖(Maven/Gradle都适用)

首先得确保项目里引入了GlazedList的依赖,用最新稳定版就行:

Maven配置

<dependency>
    <groupId>net.java.dev.glazedlists</groupId>
    <artifactId>glazedlists</artifactId>
    <version>1.11.0</version>
</dependency>

Gradle配置

implementation 'net.java.dev.glazedlists:glazedlists:1.11.0'

2. 定义你的数据模型类

就拿你说的人员类举例,要给字段加好getter方法,GlazedList得靠这个读取数据:

public class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // 必须的getter方法,别漏了!
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
}

3. 搭建GlazedList的核心过滤框架

GlazedList的过滤核心是FilterList,要实现多列同时过滤,得用AndMatcherEditor把各个列的过滤器组合起来——它能保证只有同时满足所有文本框条件的数据才会被保留。

// 1. 创建原始数据的事件列表(GlazedList推荐用这个来管理数据)
EventList<Person> personEventList = new BasicEventList<>();
// 先加点测试数据进去
personEventList.add(new Person("John", "Doe"));
personEventList.add(new Person("Jane", "Smith"));
personEventList.add(new Person("John", "Smith"));

// 2. 创建组合过滤器编辑器,用来把多列的过滤规则拼在一起
AndMatcherEditor<Person> combinedMatcherEditor = new AndMatcherEditor<>();
// 用这个组合编辑器生成最终的过滤列表
FilterList<Person> filteredPersonList = new FilterList<>(personEventList, combinedMatcherEditor);

4. 为每列创建独立的文本过滤器

给First name和Last name分别做对应的文本过滤规则,然后把它们加到组合编辑器里:

// 处理First name列的过滤器
TextFilterMatcherEditor<Person> firstNameFilter = new TextFilterMatcherEditor<>(
    filteredPersonList,
    new TextFilterator<Person>() {
        @Override
        public void getFilterStrings(List<String> baseList, Person person) {
            // 把名字转成小写,实现不区分大小写的过滤,体验更好
            baseList.add(person.getFirstName().toLowerCase());
        }
    }
);
// 设置匹配模式,这里用包含匹配(默认就是这个,也可以改成STARTS_WITH前缀匹配)
firstNameFilter.setMode(TextFilterMatcherEditor.CONTAINS);

// 处理Last name列的过滤器,逻辑和上面一样
TextFilterMatcherEditor<Person> lastNameFilter = new TextFilterMatcherEditor<>(
    filteredPersonList,
    new TextFilterator<Person>() {
        @Override
        public void getFilterStrings(List<String> baseList, Person person) {
            baseList.add(person.getLastName().toLowerCase());
        }
    }
);
lastNameFilter.setMode(TextFilterMatcherEditor.CONTAINS);

// 把两个过滤器加到组合编辑器里,这样它们就会同时生效
combinedMatcherEditor.addMatcherEditor(firstNameFilter);
combinedMatcherEditor.addMatcherEditor(lastNameFilter);

5. 绑定文本框到过滤器

把你的两个文本框(比如firstNameTextField和lastNameTextField)和对应的过滤器绑定,这样输入文本时会自动触发过滤:

// 绑定First name文本框到对应的过滤器
GlazedListsSwing.installTextField(firstNameTextField, firstNameFilter);
// 绑定Last name文本框到对应的过滤器
GlazedListsSwing.installTextField(lastNameTextField, lastNameFilter);

6. 把过滤后的列表绑定到表格

最后用GlazedList的表格模型把过滤后的列表绑定到JTable,表格会自动跟着过滤结果刷新:

// 定义表格列的显示名称和对应的字段名
String[] columnNames = {"First name", "Last name"};
String[] propertyNames = {"firstName", "lastName"};
TableFormat<Person> personTableFormat = GlazedLists.tableFormat(Person.class, columnNames, propertyNames);

// 创建GlazedList的事件表格模型
EventTableModel<Person> tableModel = new EventTableModel<>(filteredPersonList, personTableFormat);

// 绑定到你的JTable
JTable personTable = new JTable(tableModel);

几个关键小提示

  • 要是哪天想改成“满足任意一列条件就显示”,把AndMatcherEditor换成OrMatcherEditor就行,超级方便。
  • TextFilterator里转小写是为了实现不区分大小写的过滤,要是你需要严格区分大小写,去掉toLowerCase()就行。
  • 全程用EventList和EventTableModel的好处是,数据变化时表格会自动刷新,不用手动调用刷新方法。

这样一套流程走下来,你的表格就能完美实现每列独立文本框过滤,并且只显示同时符合所有条件的数据啦!

内容的提问来源于stack exchange,提问作者Cherple

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:23:10