[英]Setting ChoiceBox item too slow in javafx
i have a problem with setting item in ChoiceBox
, so basicly i must load data from a database i do it in another thread : 我在
ChoiceBox
设置项目时ChoiceBox
,因此基本上我必须从数据库中加载数据,然后在另一个线程中进行操作:
final Service<ObservableList<Country>> countryService = new Service<ObservableList<Country>>() {
@Override
protected Task<ObservableList<Country>> createTask() {
return new Task<ObservableList<Country>>() {
@Override
protected ObservableList<Country> call() throws Exception {
Dao<Country, Integer> countriesDao = null;
List<Country> result = null;
try {
countriesDao = DaoManager.createDao(Connection.getNewInstance(), Country.class);
System.out.println("getting data");
result = countriesDao.queryForAll();
System.out.println("got data");
} catch (SQLException ex) {
Logger.getLogger(ListClientsController.class.getName()).log(Level.SEVERE, null, ex);
}
return FXCollections.observableArrayList(result);
}
};
}
};
countryService.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
// taking a lot of time here like 4-5 second and freeze
// the gui(normal because it executed in Javafx Application Thread
// but why its take so much time??
cbSearchCountry.setItems(countryService.getValue());
}
});
countryService.start();
Normally database access should take a time longer that setting a list to a ChoiceBox, but no here fetching 150 record from my database is instantaneous but settings observable list to my ChoiceBox take about 5 seconds why? 通常,数据库访问所花的时间要比将列表设置为ChoiceBox所花费的时间更长,但是从我的数据库中获取150条记录不是瞬时的,但是为我的ChoiceBox设置可观察列表大约需要5秒钟,为什么? because i have too much Node in my current Scene??
因为我当前场景中的节点过多?
仅当您有<10个项目时才使用ChoiceBox,否则请使用ComboBox
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.