JavaFx ProgressBar not updating

I am trying to figure out how to update a ProgressBar in a multi-threaded environment. I am doing something wrong, but I do not understand what it is. This should just fill the panel every 3 seconds, but it is not:

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

What am I missing?

+5
source share
4 answers

Your sample is great for me.

The sample fills the bar a few times every 3 seconds, completely filling the bar after half a minute.

I just wrapped it in some kind of forest code to make it executable, and it worked without changes (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();
  }
}

You may have used the early access version of Java 8 in which a bug (now fixed) was found around ProgressBar updates.

RT-29018 ProgressBar ProgressIndicator progressProperty

+8

JDK 8 Early-Access? , . , : http://javafx-jira.kenai.com/browse/RT-29018

, css. , , , , , .

, , progressBar , updateProgress . , , .

, jdk7 , , , css CSS:

/*hack to get progress bar working. From: JDK7u17 jfxrt.jar!/com/sun/javafx/scene/control/skin/caspian/caspian.css */

/*******************************************************************************
 *                                                                             *
 * ProgressBar                                                                 *
 *                                                                             *
 ******************************************************************************/

.progress-bar {
    -fx-skin: "com.sun.javafx.scene.control.skin.ProgressBarSkin";
    -fx-background-color:
        -fx-box-border,
        linear-gradient(to bottom, derive(-fx-color,30%) 5%, derive(-fx-color,-17%));
    -fx-background-insets: 0, 1;
    -fx-indeterminate-bar-length: 60;
    -fx-indeterminate-bar-escape: true;
    -fx-indeterminate-bar-flip: true;
    -fx-indeterminate-bar-animation-time: 2;
    -fx-focus-traversable: true;
}

.progress-bar .bar {
    -fx-background-color:
        -fx-box-border,
        linear-gradient(to bottom, derive(-fx-accent,95%), derive(-fx-accent,10%)),
        linear-gradient(to bottom, derive(-fx-accent,38%), -fx-accent);
    -fx-background-insets: 0, 1, 2;
    -fx-padding: 0.416667em; /* 5 */
}

.progress-bar:indeterminate .bar {
    -fx-background-color: linear-gradient(to left, transparent, -fx-accent);
}

.progress-bar .track {
     -fx-background-color:
        -fx-box-border,
        linear-gradient(to bottom, derive(-fx-color,-15%), derive(-fx-color,2.2%) 20%, derive(-fx-color,60%));
    -fx-background-insets:  0, 1;
}

.progress-bar:disabled {
    -fx-opacity: 1.0
}
+1

If you defined updProg in the FXML file, the problem may be initialized here.

try deleting this line:

updProg = new ProgressBar();
0
source
 Timeline updateProgressBar = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                      /*Where Seconds and TotalSeconds are counter and total
                      respectively, also put somewhere Seconds++ or 
                      however you want to update progress. This time line 
                      will reapit each second */
                      progressBar.setProgress(Seconds / TotalSeconds);
                }
    }));
    updateProgressBar.setCycleCount(Timeline.INDEFINITE);
    updateProgressBar.play();
0
source

All Articles