Javafx: Re-sorting a column in a TableView

前端 未结 8 1792
春和景丽
春和景丽 2020-12-09 05:22

I have a TableView associated to a TreeView. Each time a node in the TreeView is selected, the TableView is refreshed with different data.

I am able to sort any col

8条回答
  •  孤街浪徒
    2020-12-09 05:52

    Ok, I found how to do it. I will summarize it here in case it is useful to others:

    Before you update the contents of the TableView, you must save the sortcolum (if any) and the sortType:

            TableView rooms;
            ...
            TableColumn sortcolumn = null;
            SortType st = null;
            if (rooms.getSortOrder().size()>0) {
                sortcolumn = (TableColumn) rooms.getSortOrder().get(0);
                st = sortcolumn.getSortType();
            }
    

    Then, after you are done updating the data in the TableView, you must restore the lost sort-column state and perform a sort.

           if (sortcolumn!=null) {
                rooms.getSortOrder().add(sortcolumn);
                sortcolumn.setSortType(st);
                sortcolumn.setSortable(true); // This performs a sort
            }
    

    I do not take into account the possibility of having multiple columns in the sort, but this would be very simple to do with this information.

提交回复
热议问题