Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListProperty<T> vs. ObjectProperty<ObservableList<T>>

Tags:

java

javafx

I noticed that itemsProperty in ListView is of type ObjectProperty<ObservableList<T>>, but I would have expected ListProperty<T>.

Now I wonder: When should I Use ObjectProperty<ObservableList<T>>, and when ListProperty<T>?

like image 652
Hannes Avatar asked Mar 02 '23 14:03

Hannes


1 Answers

The answer can be found in the JavaFX documentation for ObjectProperty, specifically this line:

For specialized implementations for ObservableList, ObservableSet and ObservableMap that also report changes inside the collections, see ListProperty, SetProperty and MapProperty, respectively.

So using ObjectProperty<ObservableList<T>> will report if the ObservableList itself changes, but not if any members within that ObservableList are changed.

So, to answer your question, you would use ObjectProperty<ObservableList<T>> when you only need to be reported if the ObservableList has changed (ie: items added or deleted). You should use ListProperty<T> if you also need to track changes made to the actual items within that ObservableList.

like image 118
Zephyr Avatar answered Mar 11 '23 11:03

Zephyr