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>());
有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top