How to Keep Track of row index when JTable has been sorted by the user?

前端 未结 3 1093
借酒劲吻你
借酒劲吻你 2021-01-11 15:38

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

3条回答
  •  鱼传尺愫
    2021-01-11 16:06

    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)

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

    Edit: 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)

提交回复
热议问题