在 JTable 中搜索
我使用 Java Swing 创建了一个对话框窗口,该窗口在 JTable 中显示项目列表。我想实现某种搜索功能。谁能建议我实现此类功能的最佳方法?
I have created with Java Swing a dialog window which presents in a JTable a list of items. I would like to implement some sort of search functions. Can anyone suggest me the best way to implement such feature?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读 JTable API 并点击有关“如何使用表”的 Swing 教程的链接。在那里,您将找到有关“排序和过滤”的部分,其中提供了如何使用文本字段搜索包含指定文本的行的示例。
Read the JTable API and follow the link to the Swing tutorial on "How to Use Tables". There you will find a section on "Sorting and Filtering" which gives an example of how to use a text field to search for rows containing the specified text.
这是在 JTable 中实现搜索的一种方法:
This is a way to implement search in a JTable:
看一下 SwingX 及其
JXTable
。 SwingX 提供了一组扩展普通 Swing 组件并向其添加额外功能的组件。我最喜欢的是MultiSplitPane
(它是 JSplitPane 的替代品,允许您将窗格划分为任意数量的可调整大小的部分)和JXTable
,它就像JTable< /code>,而且还具有内置搜索功能(并绑定到 Ctrl-F),您可以对行进行排序/过滤。非常整洁的东西。您需要做的就是导入库,将
JTable
更改为JXTable
(以相同的方式启动),瞧!希望有帮助。
Take a look at SwingX, and its
JXTable
. SwingX provides a set of components that extend normal Swing components and adds extra functionality to them. My favorites areMultiSplitPane
(which is an alternative to JSplitPane and allows you to divide panes in any number of resizable sections) andJXTable
which is just like aJTable
, but also has search functionality built-in (and bound to Ctrl-F) and you can sort/filter the rows. Pretty neat stuff. All you need to do is import the library, change yourJTable
toJXTable
(initiated the same way), and voila!Hope that helps.