Skip to content

Instantly share code, notes, and snippets.

@andytill
Created July 15, 2012 10:31
Show Gist options
  • Save andytill/3116203 to your computer and use it in GitHub Desktop.
Save andytill/3116203 to your computer and use it in GitHub Desktop.
Getting observable collections working with an extractor callback
import javafx.beans.InvalidationListener;
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.Arrays;
import java.util.List;
public class TestApp2 {
public static void main(String[] args) {
List<MyObservable> arrayList = Arrays.asList(new MyObservable("Test"), new MyObservable("Test2"));
ObservableList<MyObservable> list = FXCollections.observableArrayList(new Callback<MyObservable, Observable[]>() {
@Override
public Observable[] call(MyObservable myTreeData) {
return new Observable[]{myTreeData.nameProperty()};
}
});
list.addListener(new ListChangeListener<MyObservable>() {
@Override
public void onChanged(Change change) {
System.out.println(change);
}
});
list.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable arg0) {
System.out.println(arg0);
}});
list.addAll(arrayList);
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment