【问题标题】:javafx listview and treeview controls are not repainted correctlyjavafx listview 和 treeview 控件未正确重绘
【发布时间】:2017-07-30 12:01:44
【问题描述】:

我正在尝试使用 javafx 将元素放在列表视图和树视图上,但两个控件都不会刷新它们的内容。我正在使用一个可观察的列表来控制项目,每次我删除一个项目时,列表视图或树视图都会将其从数据源中删除。但视图没有更新。我仍然看到所有的项目。唯一的区别是,被删除的项目不能再被选中。例如链接 2 显示了折叠的项目列表。图 1 显示了折叠之前的项目。项目已折叠,但旧条目仍然可见。有没有人知道这个问题的解决方案。谢谢大家帮助我

链接 1:treeview is not collapsed 链接2:treeview is collapsed but not updating old view

这是我用来显示列表视图的自定义单元工厂:

public ListCell<T> call(final ListView<T> param) {
        ListCell<T> cell = new ListCell<T>(){
            @Override
            protected void updateItem(final T persistentObject, final boolean empty) {
                super.updateItem(persistentObject, empty);
                if(persistentObject instanceof POProcessStep){
                    POProcessStep poProcessStep = (POProcessStep) persistentObject;
                    if (persistentObject != null) {
                        super.setText(poProcessStep.getId() + " - " + poProcessStep.getTitle());
                    }
                }else if(persistentObject instanceof POProcess){
                    POProcess poProcess = (POProcess) persistentObject; 
                    if (persistentObject != null) {
                        super.setText(poProcess.getId() + " - " + poProcess.getTitle());
                    }
                }else if(persistentObject instanceof POCategory){
                    POCategory poCategory = (POCategory) persistentObject;
                    if(persistentObject != null){
                        super.setText(poCategory.getId() + " - " + poCategory.getTitle());
                    }
                }else if(persistentObject instanceof String){
                    if(persistentObject != null){
                        super.setText(String.valueOf(persistentObject));
                    }
                }
                super.setGraphic(null);
            }
        };
        return cell;
    }

【问题讨论】:

  • 您使用的是自定义单元工厂吗?如果是这样,请发布您的代码。

标签: java javafx treeview repaint


【解决方案1】:

您的单元工厂的updateItem(...) 需要处理单元为空的情况。这正是当一个项目被移除(或因为TreeView 中的一个节点被折叠而变为空)并且之前显示项目的单元格被重用为空单元格时的场景:

public ListCell<T> call(final ListView<T> param) {
    ListCell<T> cell = new ListCell<T>(){
        @Override
        protected void updateItem(final T persistentObject, final boolean empty) {
            super.updateItem(persistentObject, empty);
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                // ... rest of your code.
            }
       }
    }
    return cell ;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-25
  • 2023-03-27
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多