JavaFX - Observable Collections in the data model classes
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 method, not only will the content of the list be shown in the Gui component, but also if items are added to the instance afterwards, the will be refreshed automatically and will show the newly added items automatically, without the need to call any additional or methods.setItems
ListView
ObservableList
ListView
add
refresh
So far, so good. But what if I add something to the original list (i.e. ). These changes are not propagates automatically. It makes perfect sense, but it is inconvenient sometimes.list1
Of course, in a classic Java application the Model of the application does not consist of instance. So, whenever you add something to the model, you will always have to update the instances that were derived from the original list. Apparently this is inevitable, right ?ObservableCollection
ObservableLists
This got me wondering, is it a clever idea to modify type occurrences (e.g. , , , , ...) in the Model classes and replace them by their alternatives from now on ? Collection
List
Collection
Set
Iterable
ObservableCollection
Until now I always figured that these classes were only supposed to be used in the Gui layer of the applicaiton, but it seems pretty convenient to use them about everywhere.ObservableCollection