Currently I am working with database on javafx and have no idea about ObservableList, can I use ArrayList instead of ObservableList?
That depends. If you need a ObservableList
, you cannot use ArrayList
directly. ObservableList
adds a way to listen for changes on a list which ArrayList
does not implement.
However you could use a ArrayList
as backing list of a ObservableList
ArrayList<T> list = ...
ObservableList<T> observableList = FXCollections.observableList(list);
Note that in this case you should make sure you're not modifying the list through any means but observableList
, since otherwise the listeners won't be triggered.
Note that FXCollections
also provides a method for creating a ObservableList
backed by a ArrayList
without having to deal with the ArrayList
itself:
ObservableList<T> observableList = FXCollections.observableArrayList();
ArrayList: Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null.
ObservableList: A list that allows listeners to track changes when they occur.
It depends of your case. If you want to show this list in for example tableView or other view then you should use Observable collection which contains listeners ect and other components necessery for doing interaction with view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With