JTable 中的自动过滤

发布于 2024-09-12 09:55:55 字数 97 浏览 2 评论 0原文

我有一个标准的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落在眉间の轻吻 2024-09-19 09:55:55

是的,这是可能的!

启动 SwingSet3 演示(来自 Swinglabs): http://download.java.net/javadesktop /swingset3/SwingSet3.jnlp

在数据类别中,有一个 JTable 演示,其中列出了奥斯卡候选人和获奖者。

标题为“搜索标题和收件人”的文本字段正是您想要的。
SwingSet3 演示甚至允许您通过组合框条目“设置搜索过滤器”突出显示代码片段。

第一个片段中的 RowFilter 忽略前导“The”和“A”以匹配更多电影标题。我将用此替换 RegEx 模式,

".*" + filterString + ".*" 

以获得真正的“在输入时找到所有内容”的用户体验。

第二个代码段向您展示了如何实现 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

".*" + filterString + ".*" 

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.

枫以 2024-09-19 09:55:55

是的,这是可能的。阅读 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文