JTable 中的自动过滤
我有一个标准的 JTable,里面有某些数据。我想创建一个自动过滤功能:当用户在特定文本字段中输入单词时,过滤过程会自动启动,无需单击按钮。在 Java Swing 中可以实现吗?
I have a standard JTable with certain data inside. I would like to create an automatic filtering function: as the user types in a specific textfield the words the filtering process is started automatically, without the need of a clicking a button. Is it possible in Java Swing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的!
启动 SwingSet3 演示(来自 Swinglabs): http://download.java.net/javadesktop /swingset3/SwingSet3.jnlp
在数据类别中,有一个 JTable 演示,其中列出了奥斯卡候选人和获奖者。
标题为“搜索标题和收件人”的文本字段正是您想要的。
SwingSet3 演示甚至允许您通过组合框条目“设置搜索过滤器”突出显示代码片段。
第一个片段中的 RowFilter 忽略前导“The”和“A”以匹配更多电影标题。我将用此替换 RegEx 模式,
以获得真正的“在输入时找到所有内容”的用户体验。
第二个代码段向您展示了如何实现
DocumentListener
以在每次击键时刷新表过滤器。Yes, it is possible!
Start the SwingSet3 Demo (from Swinglabs): http://download.java.net/javadesktop/swingset3/SwingSet3.jnlp
In the Data category there is a JTable demo listing Oscar candidates and winners.
The TextField with the title "Search Titles and Recipients" is exactly doing what you want.
The SwingSet3 Demo even lets you highlight the code snippets via the ComboBox-entry "Setup search filter".
The RowFilter in the first snippet ignores leading "The "s and "A "s to match more movie titles. I would replace the RegEx pattern with this
to get a real "find everything while I type" user experience.
The second snippet shows you how to implement a
DocumentListener
to refresh the table filter on every keystroke.是的,这是可能的。阅读 JTable API 并点击有关“如何使用表”的 Swing 教程的链接,您将在其中找到执行此操作的过滤的工作示例。
Yes its possible. Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables", where you will find a working example of filtering that does this.