Javafx:重新排序TableView中的一列 [英] Javafx: Re-sorting a column in a TableView

查看:197
本文介绍了Javafx:重新排序TableView中的一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与TreeView关联的TableView.每次在TreeView中选择一个节点时,都会使用不同的数据刷新TableView.

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

我能够对TableView中的任何列进行排序,只需按相应的列标题即可.很好.

I am able to sort any column in the TableView, just pressing the corresponding column header. That works fine.

但是:当我在树视图中选择其他节点时,尽管列标题始终显示为已排序.数据不是.

But: when I select a different node in the tree-view, eventhough the column headers keep showing as sorted. The data is not.

是否有一种方法可以以编程方式强制用户每次更改数据时做出的排序顺序?

Is there a way to programmatically enforce the sort order made by the user each time the data changes?

推荐答案

好,我找到了解决方法.如果对其他人有用,我将在这里进行总结:

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

之前,必须先更新sortcolum(如果有)和sortType:

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();
        }

然后,完成TableView中的数据更新后,必须还原丢失的排序列状态并执行排序.

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.

这篇关于Javafx:重新排序TableView中的一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆