javafx listview和treeview控件未正确重绘

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


链接1:treeview未折叠 链接2: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(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;

    }


慕田峪7331174
浏览 694回答 1
1回答

九州编程

您的单元工厂updateItem(...)需要处理单元为空的情况。这就是删除某项(或由于TreeView折叠了一个节点而将其变为空)并将先前显示该项目的单元格重用为空单元格的情况:public ListCell<T> call(final ListView<T> param) {&nbsp; &nbsp; ListCell<T> cell = new ListCell<T>(){&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; protected void updateItem(final T persistentObject, final boolean empty) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.updateItem(persistentObject, empty);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (empty) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setText(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setGraphic(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... rest of your code.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }&nbsp; &nbsp; return cell ;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java