How to monitor changes on objects contained in an ObservableList JavaFX


I really have difficulties to understand how the ObservableList object is working in JavaFX. I want to monitor if an object in the List has been modified. So far, I only see that I can monitor if the List, as an entity itself, has been modified... but not the objects within the List:

ObservableList<Stuff> myList = FXCollections.<Stuff>observableArrayList();

myList.addListener((ListChangeListener.Change<? extends Stuff> change) -> {
            System.out.println("Update detected");
        else if(change.wasPermutated()){

            for (Stuff remitem : change.getRemoved()) {
                //do things
            for (Stuff additem : change.getAddedSubList()) {
                //do things
someStuff.setThing("clobber"); // trigger listener

Is there a way to do this? I'm looking for a workflow such that a modification to the object triggers → modification on the list → refresh on a some view.


4/24/2018 5:35:24 AM

If you want to monitor changes of the objects inside the list instead of the list itself, then you have to attach listeners to the objects of the list and not to the list.

Of course to be able to do this, the objects must support this. java.lang.Object does not support this.

Instead take a look at the ObservableValue interface. Objects that implement this interface support this kind of monitoring you're looking for. The javadoc page of ObservableValue lists all the JavaFX built-in classes that implement this interface (the list is quite looooong).

Either you have to use any of those, or you have to implement the interface yourself. And add your change listeners to the objects and not to the list.

11/10/2014 8:35:01 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow