JTable 使用双表排序以在 Java 1.4 中保持同步
有没有一种方法可以让我传递父 JTable 排序值并在 Java 1.4 中设置子 JTable 的排序?我拥有的是一个包含大约 6 列的主题的父 JTable 和一个子 JTable,它是具有相同列的父 JTable 的子集。我需要做的是,当用户从父 JTable 中选择一个项目并更新子 JTable 时,排序将更改为当时父 JTable 的排序。我在 Java 1.6 和 TableRowSorter 类中工作得很好,直到我意识到我必须使用 Java 1.4 - TableRowSorter 类不存在。
Is there a way where I can have a parent JTable sort value be passed and set the sort of a child JTable in Java 1.4? What I have is a parent JTable of subjects with about 6 columns and a child JTable that is a subset of the parent JTable with the same columns. What I need to do is when the user selects a item from the parent JTable and updates the child JTable, the sort is changed to whatever the parent JTable is at that time. I had this working fine with Java 1.6 with the TableRowSorter class until I realized I had to use Java 1.4 - which the TableRowSorter class is non-existent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您需要像
TableRowSorter
那样为表提供排序,还是需要将排序键从父级传递给子级?如果是前者,您可以使用包含实现 Comparable 的行对象的
TableModel
支持您的JTables
。然后,您可以为每列实现您自己的Comparator
类。然后,您可以在自定义TableModel
中为您的支持排序集合设置适当的Comparator
。如果是后者,我想您正在实现 ListSelectionListener 以便触发使用正确数据填充子表。在同一过程中,您可以查询父表的当前
Comparator
,并将其应用到子表模型。Is the problem that you need to provide a sort for your tables the way that
TableRowSorter
did, or that you need to pass the sort key from the parent to the child?If the former, you could back your
JTables
with aTableModel
s that contain row objects that implementComparable
. You can then implement your ownComparator
classes per column. You can then, in your customTableModel
set the appropriateComparator
on your backing sorted Collection.If the latter, I imagine that you are implementing
ListSelectionListener
in order to trigger the population of the child table with the correct data. In this same process you can query the parent table for its currentComparator
and apply that to the child table model.