在javafx中设置ChoiceBox项目太慢

时间:2015-09-12 02:24:24

标签: java multithreading user-interface concurrency javafx

我在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();

通常数据库访问应该花费更长的时间将列表设置为ChoiceBox,但是这里没有从我的数据库中取出150条记录是瞬时的,但设置可观察列表到我的ChoiceBox需要大约5秒为什么? 因为我当前的场景中有太多的节点?

1 个答案:

答案 0 :(得分:0)

仅当您拥有&lt;时才使用ChoiceBox。 10个项目,否则使用ComboBox