4 Replies Latest reply: Oct 23, 2012 12:53 AM by 969438 RSS

    How does the extractor in FXCollections.observableList work?

    csh
      How is the "extractor" supposed to work for ObservableLists?

      According to the JavaDoc it should trigger a update:
      >
      This list also reports mutations of the elements in it by using extractor. Observable objects returned by extractor (applied to each list element) are listened for changes and transformed into "update" change of ListChangeListener.
      >

      This code however does not trigger a change:
      import javafx.beans.Observable;
      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.collections.FXCollections;
      import javafx.collections.ListChangeListener;
      import javafx.collections.ObservableList;
      import javafx.util.Callback;
      
      import java.util.ArrayList;
      import java.util.List;
      
      
      public class TestApp2 {
      
          public static void main(String[] args) {
      
              List<MyObservable> arrayList = new ArrayList<MyObservable>();
              arrayList.add(new MyObservable("Test"));
              arrayList.add(new MyObservable("Test2"));
      
              ObservableList<MyObservable> list = FXCollections.observableList(arrayList, new Callback<MyObservable, Observable[]>() {
                  @Override
                  public Observable[] call(MyObservable myTreeData) {
                      Observable[] observables = new Observable[1];
                      observables[0] = myTreeData.nameProperty();
                      return observables;
                  }
              });
      
      
              list.addListener(new ListChangeListener<MyObservable>() {
      
                  @Override
                  public void onChanged(Change change) {
                      System.out.println("change");
                  }
              });
      
              arrayList.get(0).nameProperty().set("Hi");
      
          }
      
          private static class MyObservable {
      
              public MyObservable(String name) {
                  this.name.set(name);
              }
      
              public StringProperty name = new SimpleStringProperty();
      
              public StringProperty nameProperty() {
                  return name;
              }
          }
      }