Live sorting of JTable

前端 未结 4 1860
离开以前
离开以前 2020-12-28 23:14

I\'ve figured out how to get a JTable to be sorted properly, but I can\'t figure out how to get it to automatically update the sort order when a table cell is c

4条回答
  •  轮回少年
    2020-12-28 23:41

    There are several things you have to do here.

    1. Since the table model wraps your collection it has to be sortable. That means that your object (row) has to implement Comparable interface so collection can be properly sorted.
    2. In your setValueAt method you have to to update appropriate attribute and resort the collection using Collections.sort. Then, obviously, you have to call fireTableDataChanged to let table know that it needs to redraw.
    3. Same thing suppose to happen on adding data.
    4. When data is removed you don't have to resort, but still have to fireTableDataChanged
    5. If your collection is to big, you may think about adding data to appropriate place initially instead of resorting.

    Hope this helps

提交回复
热议问题