java - Java GlazedList 单列过滤
问题描述
我目前在各个列标题中有一个带有文本字段的表格,它根据每个单独的列进行过滤。我正在尝试使用 glazed 列表,但我该怎么做?每列都应该有一个单独的文本字段,并且过滤是基于所有基于各个列过滤的文本字段完成的。EG:包含“名字”和“姓氏”列的表格。该表将根据人过滤结果,名字基于名字过滤器,姓氏基于姓氏过滤器
解决方案
您需要了解的第一件事是 GlazedLists 主要用于处理对象列表——线索就在名称中。许多人将其视为一个为表格和列表添加排序和过滤功能的库,这肯定会引起头痛。
因此,首先关注 GlazedLists 将为您提供一种特殊类型的列表结构,称为 an EventList
,这是您的核心数据结构;从那里您将获得用于转换、排序、过滤等的有用方法。然后,因为它非常丰富,所以有易于使用的类将您的 EventList 链接到 JList 或 JTable。
一旦这一切都连接起来,对列表的更改和转换将自动传播到链接到它的任何 Swing 组件。
无论如何,这里有一个示例类,它将向您展示如何创建 EventList,然后通过 Swing 组件应用文本过滤器,然后将其全部链接到 Swing 表。(这不包括排序,或如何获得选定的项目,但文档非常好。)
使用 Java 9 和 GlazedLists 1.11 测试。
导入 ca.odell.glazedlists.*; 导入 ca.odell.glazedlists.gui.TableFormat; 导入 ca.odell.glazedlists.matchers.MatcherEditor; 导入 ca.odell.glazedlists.swing.DefaultEventSelectionModel; 导入 ca.odell.glazedlists.swing.DefaultEventTableModel; 导入 ca.odell.glazedlists.swing.TextComponentMatcherEditor; 导入 javax.swing.*; 导入 java.awt.*; 导入 java.util.List; 公共类 GlazedListsMultipleTextfields { 私有 JFrame 框架; 私有 JTable 表; 私人 JTextField txtFirstName; 私人 JTextField txtLastName; 私人事件列表人员; 私有 DefaultEventSelectionModel 选择模型; 公共 GlazedListsMultipleTextfields() { setupGui(); setupGlazedLists(); 填充列表(); frame.setVisible(true); } 私人无效 setupGui() { frame = new JFrame("GlazedLists 多个过滤器示例"); frame.setSize(600, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建面板以保存输入文本字段 txtFirstName = new JTextField(); JPanel pnlFirstName = new JPanel(new BorderLayout()); pnlFirstName.add(new JLabel("名字"), BorderLayout.WEST); pnlFirstName.add(txtFirstName, BorderLayout.CENTER); txtLastName = new JTextField(); JPanel pnlLastName = new JPanel(new BorderLayout()); pnlLastName.add(new JLabel("姓氏"), BorderLayout.WEST); pnlLastName.add(txtLastName, BorderLayout.CENTER); JPanel textInputs = new JPanel(); textInputs.setLayout(new BoxLayout(textInputs, BoxLayout.Y_AXIS)); textInputs.add(pnlFirstName); textInputs.add(pnlLastName); 表 = 新的 JTable(); frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); frame.getContentPane().add(textInputs, BorderLayout.NORTH); } 私人无效填充列表(){ people.add(new Person("John", "Grisham")); people.add(new Person("Patricia", "Cornwell")); people.add(new Person("Nicholas", "Sparks")); people.add(new Person("Andy", "Weir")); people.add(new Person("Elizabeth", "George")); people.add(new Person("John", "Green")); } 私人无效 setupGlazedLists() { 人 = 新的 BasicEventList(); MatcherEditor firstNameMatcherEditor = new TextComponentMatcherEditor(txtFirstName, new FirstNameTextFilterator()); MatcherEditor lastNameMatcherEditor = new TextComponentMatcherEditor(txtLastName, new LastNameTextFilterator()); FilterList filtersFirstNames = new FilterList(people, firstNameMatcherEditor); FilterList filtersLastNames = new FilterList(filteredFirstNames, lastNameMatcherEditor); TableFormat tableFormat = GlazedLists.tableFormat(new String[]{"firstName", "lastName"}, new String[]{"First Name", "Last Name"}); DefaultEventTableModel 模型 = new DefaultEventTableModel(filteredLastNames, tableFormat); selectionModel = new DefaultEventSelectionModel(filteredLastNames); table.setModel(模型); table.setSelectionModel(selectionModel); } 类 FirstNameTextFilterator 实现 TextFilterator { @覆盖 public void getFilterStrings(List list, Person person) { list.add(person.getFirstName()); } } 类 LastNameTextFilterator 实现 TextFilterator { @覆盖 public void getFilterStrings(List list, Person person) { list.add(person.getLastName()); } } 公共类人{ 私人字符串名; 私人字符串姓氏; 公共人员(字符串名字,字符串姓氏){ this.firstName = 名字; this.lastName = 姓氏; } 公共字符串 getFirstName() { 返回名字; } 公共字符串 getLastName() { 返回姓氏; } } 公共静态无效主要(字符串[]参数){ javax.swing.SwingUtilities.invokeLater(new Runnable() { @覆盖 公共无效运行(){ 新的 GlazedListsMultipleTextfields(); } }); } }
推荐阅读
- node.js - passport.js 本地策略的 IIS 和 node.js 配置
- botframework - 从 json 生成 cs 类时,LUISGen 抛出异常
- python - 让多个 matplotlib 轴使用相同的颜色循环器
- java - 自定义 Null-Safety 注释在 kotlin 中无效
- xslt - 使用 groovy 脚本或 xslt 删除 xml 版本和其他标签
- r - 如何使用 R 获取 Outlook 中收到的电子邮件的时间分量?
- python - 无法使用 Python 代码列出 GSuite 域中的组织单位
- r - 如何在由另一列分组的列中查找前 5 个最常出现的名称
- php - PHP 是否有不短路的布尔运算符?
- java - 使用 forloop 的斐波那契数列