Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX, Casting ArrayList to ObservableList

Is there a way to cast ArrayList to ObservableList? I would like to do it without iterating through ArrayList.

To be more specific, I am using ORMLite to get data from database, and I need ObservableList as an output of the method fetching data from DB.

Currently I am doing something like this:

ArrayList<Stavka> listStavaka = new ArrayList<>();  Dao<Stavka, Integer> stavkaDao = DaoManager.createDao(connection, Stavka.class); listStavaka = (ArrayList<Stavka>) stavkaDao.queryForAll(); ObservableList<Stavka> oListStavaka = FXCollections.observableArrayList(); for (Stavka stavka : listStavaka) {     oListStavaka.add(stavka); } 

And I would like to do something like this:

ObservableList<Stavka> listStavaka = FXCollections.observableArrayList(); Dao<Stavka, Integer> stavkaDao = DaoManager.createDao(connection, Stavka.class); listStavaka = (ObservableList<Stavka>) stavkaDao.queryForAll(); 
like image 215
Miljac Avatar asked Mar 05 '14 08:03

Miljac


People also ask

Is an ArrayList an observable list?

ArrayList is a particular implementation of List , but is not an implementation of ObservableList .

What is an ObservableList in Javafx?

ObservableList : A list that enables listeners to track changes when they occur. ListChangeListener : An interface that receives notifications of changes to an ObservableList. ObservableMap : A map that enables observers to track changes when they occur.

What is FXCollections?

public class FXCollections extends Object. Utility class that consists of static methods that are 1:1 copies of java. util. Collections methods.


2 Answers

You can do

ObservableList<Stavka> oListStavaka = FXCollections.observableArrayList(listStavaka); 
like image 121
Uluk Biy Avatar answered Oct 17 '22 08:10

Uluk Biy


As in Uluk Biy's answer, but if you don't want to mix it with new list by FXCollections, just use this...

oListStavaka.addAll(stavkaDao.queryForAll()); 
like image 25
Eric Chan Avatar answered Oct 17 '22 07:10

Eric Chan