JavaFX, Casting ArrayList to ObservableList
Asked Answered
A

2

43

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();
Abnormality answered 5/3, 2014 at 8:7 Comment(0)
V
110

You can do

ObservableList<Stavka> oListStavaka = FXCollections.observableArrayList(listStavaka);
Vallie answered 5/3, 2014 at 8:24 Comment(1)
Thank you for the answer. It really helped me.Ovotestis
N
2

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());
Nev answered 9/11, 2018 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.