Search code examples
javajavafx-2object-propertyobservablelist

Initializing ObjectProperty<ObservableList<T>>?


I have a property which wraps an ObservableList. But I cannot figure out how to initialize it. Currently I am doing it like this

ObjectProperty<ObservableList<T>> property = new ChoiceBox<T>().itemsProperty();

which is obviously totally bad practice. Another one was

ObjectProperty<ObservableList<T>> property = new SimpleObjectProperty<ObservableList<T>>();

but this needs later attention which I try to avoid to initialize the internal ObservableList with an empty List.

I search for something like this

ObjectProperty<ObservableList<T>> property = new SimpleObjectProperty<ObservableList<T>>(new ObservableList<T>());

Solution

  • I recommend you to use ListProperty instead of ObjectProperty>

    To initialize it do :
    ListProperty<Integer> listProperty = new SimpleListProperty<Integer>(FXCollections.<Integer>observableArrayList());

    For more information you can read this article ListProperty vs ObjectProperty