javafx listview and treeview controls are not repa

2020-01-23 05:50发布

问题:

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