首页 文章

javafx listview和treeview控件未正确重新绘制

提问于
浏览
3

我试图用javafx将元素放在listview和treeview上,但两个控件都不会刷新它们的内容 . 我使用一个obvservable列表来控制项目,每次删除一个项目时,listview或treeview将其从数据源中删除 . 但视图没有更新 . 我还在看所有物品 . 唯一的区别是,删除的项目不能再被选中 . 例如,链接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;
    }

1 回答

  • 13

    您的单元工厂的 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 ;
    }
    

相关问题