Java: Selected rows's index does not changes when sorted

本小妞迷上赌 提交于 2019-12-21 07:41:16

问题


I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true);. So this works on well. But I also have a methos in my JFrame class which is fired when i push a button. It gets the selected rows indexes using this code int selectedRows[] = this.table1.getSelectedRows();. And displays an edit window for the first row corresponding in the selected interval.

The problem is that if I don't click on column's headers (I mean i don't sorte them at all) my method works perfect. But when I sort the row, the indexes of the rows doesn't seems to change at all - thus resulting an edit window for the old row whicn was initially in that position before making any sort.

I am using JDK 6 could anyonw give ma a tip?


回答1:


The underlying model does not change order. Only the view changes. You can read more about this in Sun's tutorial. You will need to use JTable.convertRowIndexToView() and JTable.convertRowIndexToModel().




回答2:


You need to use convertRowIndexToView(int) and convertRowIndexToModel(int) to convert model (underlying data) indices and view indices.



来源:https://stackoverflow.com/questions/2657947/java-selected-rowss-index-does-not-changes-when-sorted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!