【问题标题】:Perform a Progress Indicator in a background task javafx在后台任务 javafx 中执行进度指示器
【发布时间】:2016-06-29 19:08:30
【问题描述】:

在任务执行过程中进度指示器运行平稳,但当任务结束时进度指示器继续转动。我希望这个任务的结束进度指示器停止。这是我的代码

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;
        }
    };
}

【问题讨论】:

    标签: java multithreading javafx progress-indicator


    【解决方案1】:

    你不能从后台线程改变 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();  
    

    【讨论】:

    • 那么您没有到达onSucceeded 处理程序,可能是因为您遇到了异常。查看更新以了解如何找出问题所在。
    猜你喜欢
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多