Add extractor to existing ObservableList

2019-06-24 02:24发布

I have a model class ModelParent that has an observable list ObservableList<ModelChild>, where ModelChild is another model class, with it's own properties.

class ModelParent { 
    private final ObservableList<ModelChild> children = FXCollections.observableArrayList(); 

    public ObservableList<ModelChild> getChildren() {
        return children;
    }

    // More properties...
}

In certain occasions, I want to have an extractor applied to the list - that is, to be able to be notified when the list changes, or when a certain member of the list changes. The problem is, in different occasions I will want different properties to be watched, and sometimes I don't care at all about them.

I know I can apply an extractor to an ObservableList when I create it, but is it possible to add an extractor to an existing list? Or maybe create an observable list backed by the original (with changes reflected both ways), but with an additional extractor?

I saw this, but it explicitly says that changes done directly to the backing list will not trigger change events. The problem is the same model object instance may be held by a different object that doesn't care about this new extractor, and will happily add or remove items on the original ObservableList.

Does such thing exist in Java 8? Or is there a simple way to implement it myself?

0条回答
登录 后发表回答