4 Replies Latest reply: Feb 24, 2013 6:54 AM by Rafal Z RSS

    Progressbar not forwarding

    Rafal Z
      Hallo,

      I'm tying to use progressbar in my application, but it behaves not correctly. It doesn't show progress, I mean I'm not able to see the movement of the bar while iterating (myprogressbar.setProgress(k), where k is float), I can see only the final result when it fills completly its area. How to use the control correctly?

      with regards

      Rafal Ziolkowski
        • 1. Re: Progressbar not forwarding
          James_D
          It sounds like you are running the process you're monitoring in the FX Application thread; though it's hard to tell what the issue is without some code. Can you post a simple, self-contained, executable example which shows the problem?
          • 2. Re: Progressbar not forwarding
            Rafal Z
            Yes that's right. So I'm using now Platform.invokeLater but without success, the behaviour of progressbar doesn't change. I tried this with the simplest project: I created new project with whizard of Netbeans 7.2.1:

            /*
            * To change this template, choose Tools | Templates
            * and open the template in the editor.
            */
            package javafxapplication6;

            import java.util.logging.Level;
            import java.util.logging.Logger;
            import javafx.application.Application;
            import javafx.application.Platform;
            import javafx.event.ActionEvent;
            import javafx.event.EventHandler;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.control.ProgressBar;
            import javafx.scene.layout.BorderPane;
            import javafx.scene.layout.StackPane;
            import javafx.stage.Stage;

            /**
            *
            * @author rziolkowski
            */
            public class JavaFXApplication6 extends Application {
            private ProgressBar pb;
            Button btn;
            BorderPane root;
            Scene scene;
            Stage pstage;

            @Override
            public void start(Stage primaryStage) {
            pstage=primaryStage;
            btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
            System.out.println("Hello World!");
            for(float k=0.0F;k<1.0F;k+=0.1F){
            pb.progressProperty().set(k);
            System.out.println("pr: "+k);
            try {
            Thread.sleep(1000);
            } catch (InterruptedException ex) {
            //Logger.getLogger(FsDataUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            }

            }
            });
            pb=new ProgressBar();

            root = new BorderPane();
            root.setCenter(btn);
            root.setBottom(pb);


            scene = new Scene(root, 300, 250);

            pstage.setTitle("Hello World!");
            pstage.setScene(scene);
            pstage.show();
            }

            /**
            * The main() method is ignored in correctly deployed JavaFX application.
            * main() serves only as fallback in case the application can not be
            * launched through deployment artifacts, e.g., in IDEs with limited FX
            * support. NetBeans ignores main().
            *
            * @param args the command line arguments
            */
            public static void main(String[] args) {
            launch(args);
            }
            }

            As You have mentioned, it doesn't work, only after the loop finishes the progressbar shows its state.

            Know the same code with runLater:

            /*
            * To change this template, choose Tools | Templates
            * and open the template in the editor.
            */
            package javafxapplication6;

            import java.util.logging.Level;
            import java.util.logging.Logger;
            import javafx.application.Application;
            import javafx.application.Platform;
            import javafx.event.ActionEvent;
            import javafx.event.EventHandler;
            import javafx.scene.Scene;
            import javafx.scene.control.Button;
            import javafx.scene.control.ProgressBar;
            import javafx.scene.layout.BorderPane;
            import javafx.scene.layout.StackPane;
            import javafx.stage.Stage;

            /**
            *
            * @author rziolkowski
            */
            public class JavaFXApplication6 extends Application {

            private ProgressBar pb;
            Button btn;
            BorderPane root;
            Scene scene;
            Stage pstage;

            @Override
            public void start(Stage primaryStage) {
            pstage = primaryStage;
            Platform.runLater(new Runnable() {
            @Override
            public void run() {
            btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
            System.out.println("Hello World!");
            for (float k = 0.0F; k < 1.0F; k += 0.1F) {
            pb.progressProperty().set(k);
            System.out.println("pr: " + k);
            try {
            Thread.sleep(1000);
            } catch (InterruptedException ex) {
            //Logger.getLogger(FsDataUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            }

            }
            });
            pb = new ProgressBar();

            root = new BorderPane();
            root.setCenter(btn);
            root.setBottom(pb);


            scene = new Scene(root, 300, 250);

            pstage.setTitle("Hello World!");
            pstage.setScene(scene);
            pstage.show();
            }
            });
            }

            /**
            * The main() method is ignored in correctly deployed JavaFX application.
            * main() serves only as fallback in case the application can not be
            * launched through deployment artifacts, e.g., in IDEs with limited FX
            * support. NetBeans ignores main().
            *
            * @param args the command line arguments
            */
            public static void main(String[] args) {
            launch(args);
            }
            }

            the same effect, progressbar doesn't progress until the loop finishes.
            I'm using JavaFX 2.2.5 with JDK 6 update 39

            greetings

            Rafal Ziolkowski
            • 3. Re: Progressbar not forwarding
              shakir.gusaroff
              Hi. Here is an example using a task:
                 import java.util.logging.Level;
              import java.util.logging.Logger;
              import javafx.application.Application;
              import javafx.application.Platform;
              import javafx.beans.property.SimpleFloatProperty;
              import javafx.concurrent.Task;
              import javafx.event.ActionEvent;
              import javafx.event.EventHandler;
              import javafx.scene.Scene;
              import javafx.scene.control.Button;
              import javafx.scene.control.ProgressBar;
              import javafx.scene.layout.BorderPane;
              import javafx.scene.layout.StackPane;
              import javafx.stage.Stage;
              
              public class ProgressbarMovement extends Application {
              
                  Task task;
                  private ProgressBar pb;
                  Button btn;
                  BorderPane root;
                  Scene scene;
                  Stage pstage;
              
                  @Override
                  public void start(Stage primaryStage) {
                      pstage = primaryStage;
                      btn = new Button();
                      btn.setText("Say 'Hello World'");
                      pb = new ProgressBar();
              
              
                      btn.setOnAction(new EventHandler<ActionEvent>() {
                          @Override
                          public void handle(ActionEvent event) {
              
                              task = createTask();
                              pb.progressProperty().unbind();
                              pb.progressProperty().bind(task.progressProperty());
                              new Thread(task).start();
              
              
                          }
                      });
              
              
                      root = new BorderPane();
                      root.setCenter(btn);
                      root.setBottom(pb);
              
                      scene = new Scene(root, 300, 250);
              
                      pstage.setTitle("Hello World!");
                      pstage.setScene(scene);
                      pstage.show();
                  }
              
                  public Task<Integer> createTask() {
              
              
                      Task<Integer> task = new Task<Integer>() {
                          @Override
                          protected Integer call() throws Exception {
                              int iterations;
                              for (iterations = 0; iterations < 10; iterations++) {
              
                                  try {
                                      Thread.sleep(1000);
                                  } catch (InterruptedException ex) {
                                  }
              
                                  updateProgress(iterations + 1, 10);
              
              
                              }
                              return iterations;
                          }
                      };
              
                      return task;
              
              
              
                  }
              
                  public static void main(String[] args) {
                      launch(args);
                  }
              }
              • 4. Re: Progressbar not forwarding
                Rafal Z
                Thank's, that is the solution

                greetings

                Rafal Ziolkowski