0

I am aware of the factory method that takes an extractor as a parameter:

FXCollections.observableList(List<E> list, Callback<E, Observable[]> extractor)

Is there an equivalent for an ObservableSet?

If not, how can I achieve same/ similar functionality when I use an ObservableSet instead of an ObservableList?

4

2 回答 2

0

SetChangeListener.Change lacks a Update change type (there are only Add / Remove changes).

Therefore you cannot achieve this kind of behavior with a ObservableSet, simply because it's not part of the ObservableSet interface.

You'd need to define your own interface to achieve this behavior.

Note that modifying Set elements is often a bad idea, since it can mess up the way the data is stored. If the modification e.g. changes the hashCode, you won't get the correct results, if you modify an element of a HashSet

于 2017-02-27T19:15:28.297 回答
0

There isn't. In fact, where ListChangeListener.Change has a wasUpdated() method, which returns true when the change is created because one of the properties specified in the extractor has changed, SetChangeListener.Change has no such method.

If your set contains elements of type S, which has a property of type Property<T>, call it someProperty(), in which you are interested, you can do

ChangeListener<T> listener = (obs, oldValue, newValue) -> { /* some code */ };

ObservableSet<S> set = FXCollections.observableSet();
set.addListener((Change<? extends S> c) -> {
    if (c.wasAdded()) {
        c.getElementAdded().someProperty().addListener(listener);
    }
    if (c.wasRemoved()) {
        c.getElementRemoved().someProperty().removeListener(listener);
    }
});
于 2017-02-27T19:19:19.477 回答