如何在用户对JTable进行排序时跟踪行索引? [英] How to Keep Track of row index when JTable has been sorted by the user?

查看:94
本文介绍了如何在用户对JTable进行排序时跟踪行索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个第一行空白的JTable。现在,当我通过单击该列对基于列的表进行排序时,空行将位于底部。如果我在空行中插入一些东西并进行排序,则相应地放置该行。即使排序,我如何跟踪其行索引。我需要访问该行,但如果用户进行排序,那么我将松散行索引,因为它不再是第一行。

I have a JTable which has 1st row blank. Now when I sort the table based on a column by clicking on that column then the blank row goes at the bottom. If I insert something in the blank row and do the sorting then the row is placed accordingly. How do I keep track of its row index even when it is sorted. I need to access that row but if user does the sorting then i loose the row index as it is no more the 1st row.

推荐答案

假设您正在使用Java 6中添加的 TableRowSorter 内容,我认为你需要看看是在 RowSorter的方法 convertRowIndexToModel convertRowIndexToView 什么类。你会做类似

Assuming you're using the TableRowSorter stuff added in Java 6, I think what you need to look at are the methods convertRowIndexToModel and convertRowIndexToView in the RowSorter Class. You'd do something like

table.getRowSorter()。convertRowIndexToView(0)

找出哪个可见行索引实际上是你模型中的行索引0.

to find out which visible row index is actually row index 0 from your model.

编辑:正如Tulskiy在评论中指出的那样,如果没有为表分配行排序器,这可能会导致NullPointerException。最好直接在JTable上使用这些方法,例如 table.convertRowIndexToView(0)

As Tulskiy pointed out in the comments, this may cause a NullPointerException if no row sorter is assigned to the table. Better to use the methods directly on JTable instead, e.g. table.convertRowIndexToView(0)

这篇关于如何在用户对JTable进行排序时跟踪行索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆