簡體   English   中英

在后台任務 javafx 中執行進度指示器

[英]Perform a Progress Indicator in a background task javafx

在任務執行期間,進度指示器運行平穩,但當任務結束時,進度指示器繼續轉動。 我希望此任務結束時進度指示器停止。 這是我的代碼

nextButton.setDisable(true);
task = createWorker();          
progressInd.progressProperty().bind(task.progressProperty());
th = new Thread(task);          
th.start();                     

這是我的任務。

public Task createWorker() {
    return new Task() {
        @Override
        protected Object call() throws Exception {
            list = new LinkedList<BufferedImage>();
            list = vX.getFrame(path.getText());
            System.out.println(list.size());
            vX.getCorruptedImage((LinkedList<BufferedImage>) list);
            progressInd.progressProperty().unbind();
            progressInd.setProgress(task.getProgress());
            nextButton.setDisable(false);
            return output;
        }
    };
}

您無法從后台線程更改 UI,因此行

progressInd.progressProperty().unbind();
progressInd.setProgress(task.getProgress());
nextButton.setDisable(false);

不應在您的call()方法中執行。

相反,你應該做

nextButton.setDisable(true);
task = createWorker();          
progressInd.progressProperty().bind(task.progressProperty());
task.setOnSucceeded(e -> {
    progressInd.progressProperty().unbind();
    progressInd.setProgress(1); // mark as complete...
    nextButton.setDisable(false);
});
// always good practice to (at a minimum) log exceptions if they occur:
task.setOnFailed(e -> task.getException().printStackTrace());
th = new Thread(task);          
th.start();  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM