JavaFX: ListProperty and members notification

71 views Asked by At

Consider the instances of a Wrapper class. The Wrapper class has the ListProperty of ClassA instances and the ListProperty of ClassB instancies.

public class Wrapper {
    private ListProperty<ClassA> aClasses;
    private ListProperty<ClassB> bClasses;

    public ListProperty<ClassA> aClassesProperty() { return aClasses; }
    public ListProperty<ClassB> bClassesProperty() { return bClasses; }

    // Methods…
}

The instances of ClassA and ClassB need to know if they are included in some Wrapper. They may be included only in one Wrapper or not be included at all.

public class ClassA { // Similarly ClassB
    private Wrapper owner;
}

Some method of the Wrapper may remove member from one of ListProperties. I wonder if JavaFX has an automatic mechanism to notify a removed member of a ListProperty? Some binding mechanism between ListProperty of Wrapper and owner of ClassA/ClassB?

1

There are 1 answers

0
azro On

You can go by this way if you are in Wrapper class:

aClasses.getValue().addListener(
                (ListChangeListener<ClassA>)event-> {
                    if (event.next()) {
                        if (event.wasAdded()) {

                        } else if (event.wasRemoved()) {

                        }
                    }
                }
        );

Or change to : owner.aClassesProperty().getValue().addListener( if you are in ClassA/B

But for JavaFX there is : FXCollections.observableList() dedicated to Listeners on List, learn more https://docs.oracle.com/javafx/2/collections/jfxpub-collections.htm at half-page and here https://docs.oracle.com/javase/8/javafx/api/javafx/collections/ObservableList.html