How to load big file in background during initiali

2019-07-01 16:57发布

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条回答
Viruses.
2楼-- · 2019-07-01 17:07

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();
    }
查看更多
登录 后发表回答