JavaFx ProgressBar不会更新 [英] JavaFx ProgressBar doesnt update

查看:725
本文介绍了JavaFx ProgressBar不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to understand how to update a ProgressBar in a multithreaded environment. Iam doing something wrong here but I dont see what it is. This should simply fill up the bar every 3sec but it doesnt:

            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();

我缺少什么?

推荐答案

你的样品对我来说很好。

Your sample works fine for me.

样品每隔3秒钟填充一次,完全填满酒吧半个分钟。

The sample fills the bar a little bit every 3 seconds, completely filling the bar in half a minute.

我只是将它包装在一些脚手架代码中以使其可执行且无需更改(java7u15,win7)。

I just wrapped it in some scaffolding code to make it executable and it worked without change (java7u15, win7).

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






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


Perhaps you have been using some early access version of Java 8 which had a bug in it (now fixed) around ProgressBar updates.

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

这篇关于JavaFx ProgressBar不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆