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

后端 未结 5 1112
悲&欢浪女
悲&欢浪女 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:39

    Not sure if this works in JavaFX 2.2, but it does in JavaFX 8 and took me a while to figure out. You need to create your own ListViewSkin and add a refresh method like:

    public void refresh() {
        super.flow.recreateCells(); 
    }
    

    This will call updateItem without having to replace the whole Observable collection.

    Also, to use the new Skin, you need to instantiate it and set it on the initialize method of the controller in case you're using FXML:

    MySkin skin = new MySkin<>(this.listView); // Injected by FXML
    this.listView.setSkin(skin);
    ...
    ((MySkin) listView.getSkin()).refresh(); // This is how you use it    
    

    I found this after debugging the behavior of an Accordion. This controls refresh the ListView it contains everytime you expand it.

提交回复
热议问题