Constantine
Constantine

Reputation: 2032

edit getValue() on javafx property - listener not notified

I added listener like this,

FwDescriptionListView.itemsProperty().addListener((observable, oldValue, newValue) -> {
        System.out.println("Old = " + oldValue + ", new = " + newValue);
});

and edited ListProperty value like this,

FwDescriptionListView.itemsProperty().getValue().remove(descText);

Why listener wasn't notified?

Upvotes: 0

Views: 109

Answers (1)

James_D
James_D

Reputation: 209330

The ListView's itemsProperty is an ObjectProperty<ObservableList<T>> (where T is the type of object in the list). A change listener registered with it will be notified if you replace the entire list, i.e. if you do

fwDescriptionsListView.setItems(someWholeNewList);

Of course, that's not what you are doing: you are merely removing an element from the list. (Note that your code is equivalent to fwDescriptionListView.getItems().remove(descText);, which is the more common way of doing this.)

To listen for changes in the list, you register a ListChangeListener with the list:

fwDescriptionListView.getItems().addListener((ListChangeListener.Change<? extends T> change) -> {
    System.out.println("List changed");
});

where, again you replace T with the type of the elements of the list. You can get more information about what actually happened to the list (items added, items removed, etc) from the change parameter: see the Javadocs for details.

Upvotes: 1

Related Questions