javafx listview and treeview controls are not repainted correctly

与世无争的帅哥 提交于 2019-12-17 02:24:26

问题


i am trying to put elements on a listview and treeview with javafx, but both controls wont refresh theyre content. i am using an obvservable list to control the items and every time i delete one item, the listview or treeview removes it from the datasource. but the view is not updating. i am still seeing all the items. the only difference is, the removed item can not be selected any more. for example link 2 shows the collaped item list. image 1 shows the items before they are collaped. the items are collapsed but the old entry is still visible. does anybody know a solution for this problem. thank you all for helping me

link 1: treeview is not collapsed link 2: treeview is collapsed but not updating old view

this is the custom cell factory i use to display a listview:

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:


Your cell factory's updateItem(...) needs to handle the case where the cell is empty. This will be exactly the scenario when an item is removed (or becomes empty because a node in the TreeView was collapsed) and the cell that previously showed an item is reused as an empty cell:

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


来源:https://stackoverflow.com/questions/26821319/javafx-listview-and-treeview-controls-are-not-repainted-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!