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

后端 未结 5 1168
悲&欢浪女
悲&欢浪女 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条回答
  •  猫巷女王i
    2021-02-08 19:39

    For any body else that ends up on this page:

    The correct way of doing this is to supply your observable list with an "extractor" Callback. This will signal the list (and ListView) of any property changes.

    Custom class to display:

    public class Custom {
        StringProperty name = new SimpleStringProperty();
        IntegerProperty id = new SimpleIntegerProperty();
    
        public static Callback extractor() {
            return new Callback() {
                @Override
                public Observable[] call(Custom param) {
                    return new Observable[]{param.id, param.name};
                }
            };
        }
    
        @Override
        public String toString() {
            return String.format("%s: %s", name.get(), id.get());
        }
    }
    

    And your main code body:

    ListView myListView;
    //...init the ListView appropriately
    ObservableList items = FXCollections.observableArrayList(Custom.extractor());
    myListView.setItems(items);
    Custom item = new Custom();
    items.add(item);
    item.name.set("Mickey Mouse");
    // ^ Should update your ListView!!!
    

    See (and similar methods): https://docs.oracle.com/javafx/2/api/javafx/collections/FXCollections.html#observableArrayList(javafx.util.Callback)

提交回复
热议问题