Here follows a simple piece of JavaFX code, to illustrate my question.
List list1 = new ArrayList();
list1.add("foo");
...
someListView = new ListView<>();
ObservableList someObservableList = FXCollections.observableList(list1);
someListView.setItems(someObservableList);
...
someObservableList.add("bar");
If I understood correctly, after calling the setItems
method, not only will the content of the list be shown in the ListView
Gui component, but also if items are added to the ObservableList
instance afterwards, the ListView
will be refreshed automatically and will show the newly added items automatically, without the need to call any additional add
or refresh
methods.
So far, so good. But what if I add something to the original list (i.e. list1
). These changes are not propagates automatically. It makes perfect sense, but it is inconvenient sometimes.
Of course, in a classic Java application the Model of the application does not consist of ObservableCollection
instance. So, whenever you add something to the model, you will always have to update the ObservableLists
instances that were derived from the original list. Apparently this is inevitable, right ?
This got me wondering, is it a clever idea to modify Collection
type occurrences (e.g. List
, Collection
, Set
, Iterable
, ...) in the Model classes and replace them by their ObservableCollection
alternatives from now on ?
Until now I always figured that these ObservableCollection
classes were only supposed to be used in the Gui layer of the applicaiton, but it seems pretty convenient to use them about everywhere.