JavaFx ProgressBar不会更新

Chr*_*mos 6 multithreading javafx progress-bar

我尝试了解如何在多线程环境中更新ProgressBar.我在这里做错了但我不知道它是什么.这应该只是每隔3秒填满一次吧,但它没有:

    Task<Void> task = new Task<Void>(){
        @Override
        public Void call(){
            for (int i = 1; i < 10; i++)    {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i);
                updateProgress(i , 10);
            }
        return null;                
        }
    };

    updProg = new ProgressBar();
    updProg.progressProperty().bind(task.progressProperty());

    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

jew*_*sea 9

你的样本对我来说很好.

样品每3秒钟填充一次,在半分钟内完全填充棒.

我只是将它包装在一些脚手架代码中以使其可执行,并且它没有改变(java7u15,win7).

simpletaskprogress

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ProgressTest extends Application {
  public static void main(String[] args) { Application.launch(args); }
  @Override public void start(Stage stage) {
    Task<Void> task = new Task<Void>() {
      @Override public Void call() {
        for (int i = 0; i < 10; i++) {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            Thread.interrupted();
            break;
          }
          System.out.println(i + 1);
          updateProgress(i + 1, 10);
        }
        return null;
      }
    };

    ProgressBar updProg = new ProgressBar();
    updProg.progressProperty().bind(task.progressProperty());

    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();

    StackPane layout = new StackPane();
    layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
    layout.getChildren().add(updProg);

    stage.setScene(new Scene(layout));
    stage.show();
  }
}
Run Code Online (Sandbox Code Playgroud)

也许你一直在使用Java 8的一些早期访问版本,它在ProgressBar更新中有一个错误(现已修复).

更新progressProperty时,RT-29018 ProgressBar和ProgressIndicator消失