JavaFX 2.2: How to force a redraw/update of a ListView

后端 未结 5 1153
悲&欢浪女
悲&欢浪女 2021-02-08 19:16

I have a ListView control in a JavaFX 2 modal dialog window.

This ListView displays DXAlias instances, the ListCells for which are manufactured by a cell factory. The m

5条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 19:33

    For now, I am able to get the ListView to redraw and correctly indicate the selected default by using the following method, called forceListRefreshOn(), in my button handler:

    @FXML
    void handleAliasDefault(ActionEvent event) {
    
        int sel = lsvAlias.getSelectionModel().getSelectedIndex();
        if (sel >= 0 && sel < lsvAlias.getItems().size()) {
            lsvAlias.setUserData(lsvAlias.getItems().get(sel));
            this.forceListRefreshOn(lsvAlias);
        }
    }
    

    The helper method just swaps out the ObservableList from the ListView and then swaps it back in, presumably forcing the ListView to update its ListCells:

    private  void forceListRefreshOn(ListView lsv) {
        ObservableList items = lsv.getItems();
        lsv.setItems(null);
        lsv.setItems(items);
    }
    

提交回复
热议问题