Java – how to track row indexes after JTable has been sorted by users?

I have a JTable with the first line blank Now, when I sort the columns by clicking on the column, the blank row is at the bottom If I insert something into a blank line and sort it, I place it accordingly The row index can be tracked even when sorting I need to access the row, but if the user sorts, I loosen the row index because it is no longer the first row

Solution

Assuming you are using the tablerowsorter content added in Java 6, I think you need to view the methods of convertrowindextomodel and convertrowindextoview in the rowsorter class You will do something similar

table. getRowSorter(). convertRowIndexToView(0)

Find out which visible row index in your model is actually row index 0

Edit: as tulskiy pointed out in the comment, if the row sorter is not assigned to the table, it may cause a NullPointerException exception Better use methods directly on JTable, such as table convertRowIndexToView(0)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>