如何选择JTable中最后插入的行? [英] How to select the last inserted row in a JTable?

查看:122
本文介绍了如何选择JTable中最后插入的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两列的JTable和一个使用一个与哈希映射一起工作的类的表模型。这个地图的元素是表格行。

I have a JTable with two columns and a table model that uses a class which works with a hash map. The elements of this map are the table rows.

每次添加新元素时,我都需要选择元素,无论是否排序。现在,我尝试使用以下方法:

Every time I add a new element I need the element to be selected, regardless of any sorting. Now, I've tried using the following method:

int lastRow = table.convertRowIndexToView(tableModel.getRowCount() - 1);
table.setRowSelectionInterval(lastRow, lastRow);

问题是,这只适用于我按排序方式添加数据时(例如A) ,B,C,甚至不是因为D,例如,一旦添加就放在A之前)。有人能告诉我如何解决这个问题吗?

The problem is, this works only when I'm adding data in a sorted fashion (e.g. "A", "B", "C" and not even then since "D", for example, is placed ahead of A once added). Could someone tell me how to solve this problem?

推荐答案

问题很可能在你的 TableModel ,因为您发布的代码很好。您提到使用 HashMap 来存储 TableModel 的数据。 HashMap 的问题在于它没有任何排序。向其添加元素可能会改变您在地图中获取元素的顺序。

The problem is most likely in your TableModel, since the code you posted is just fine. You mention you use a HashMap for storing the data of your TableModel. Problem with a HashMap is that it hasn't any ordering. Adding an element to it might alter the order in which you get the elements in the map.

最好使用符合排序的数据结构。

Better to use a data structure which respects the ordering.

这篇关于如何选择JTable中最后插入的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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