How to load big file in background during initialize() method of Javafx?

人盡茶涼 提交于 2019-12-12 13:53:25

问题


My javafx desktop application load and big text file during the initialization of the application in the

 @Override
    public void initialize(URL url, ResourceBundle rb) {

      loadAppConfigurationFile();
   }


   private void loadAppConfigurationFile() {

        Task<Void> task = new Task<Void>() {
            @Override
            public Void call() throws InterruptedException {

                //read file and do some operation
                return null;
            }
        };
        new Thread(task).start();
    }

But the problem here is my application GUI doesn't appear till this process it over. I invoked this file reading in another thread excepting that my GUI stage/scene will be loaded first and their I can show some loading message till that file is loaded in the system.

Kindly tell the exact workaround for this.


回答1:


i try this and its work easily...here bar is a progress bar

    @FXML
    void initialize () {
        assert bar != null : "fx:id=\"bar\" was not injected: check your FXML file 'Check.fxml'.";
        loadAppConfigurationFile();
    }

    private void loadAppConfigurationFile () {
        Task task = new Task<Void>() {
            @Override
            public Void call() throws InterruptedException {
                int max = 1000000;
                for (int i = 1; i <= max; i = i + 10) {
                    if (isCancelled()) {
                        break;
                    }
                    updateProgress(i, max);
                    System.out.println("somethings is here");
                }
                return null;
            }
        };
        bar.progressProperty().bind(task.progressProperty());
        new Thread(task).start();
    }


来源:https://stackoverflow.com/questions/20091341/how-to-load-big-file-in-background-during-initialize-method-of-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!