mydomain
No ADS
No ADS

JavaFX ProgressBar and ProgressIndicator Tutorial with Examples

  1. ProgressBar and ProgressIndicator
  2. ProgressBar & ProgressIndicator Example
  3. Progress and Task

1. ProgressBar and ProgressIndicator

No ADS
ProgressBar and ProgressIndicator are UI controls that visualize progress of any operations in your JavaFX applications.
ProgressBar and ProgressIndicator with workload to be determined:

2. ProgressBar & ProgressIndicator Example

The following example creates a ProgressBar and a ProgressIndicator simulate a process not determine the time of the end.
ProgressDemo.java
package org.o7planning.javafx.progress;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class ProgressDemo extends Application {

    @Override
    public void start(Stage stage) {

        ProgressBar progressBar = new ProgressBar();
        ProgressIndicator progressIndicator = new ProgressIndicator();

        FlowPane root = new FlowPane();
        root.setPadding(new Insets(10));
        root.setHgap(10);
        root.getChildren().addAll(progressBar, progressIndicator);

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

        stage.setTitle("JavaFX ProgressBar & ProgressIndicator (o7planning.org)");

        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

3. Progress and Task

No ADS
You can create a Task object to perform certain task, such as copying files list. Copying requires take some time, you need to use ProgressBar or ProgressIndicator to display the percentage of work already done.
ProgressAndTaskDemo.java
package org.o7planning.javafx.progress;

import java.io.File;
import java.util.List;

import javafx.application.Application;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.FlowPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ProgressAndTaskDemo extends Application {

   private CopyTask copyTask;

   @Override
   public void start(Stage primaryStage) {

       final Label label = new Label("Copy files:");
       final ProgressBar progressBar = new ProgressBar(0);
       final ProgressIndicator progressIndicator = new ProgressIndicator(0);

       final Button startButton = new Button("Start");
       final Button cancelButton = new Button("Cancel");

       final Label statusLabel = new Label();
       statusLabel.setMinWidth(250);
       statusLabel.setTextFill(Color.BLUE);

       // Start Button.
       startButton.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
               startButton.setDisable(true);
               progressBar.setProgress(0);
               progressIndicator.setProgress(0);
               cancelButton.setDisable(false);

               // Create a Task.
               copyTask = new CopyTask();

               // Unbind progress property
               progressBar.progressProperty().unbind();

               // Bind progress property
               progressBar.progressProperty().bind(copyTask.progressProperty());

               // Hủy bỏ kết nối thuộc tính progress
               progressIndicator.progressProperty().unbind();

               // Bind progress property.
               progressIndicator.progressProperty().bind(copyTask.progressProperty());

               // Unbind text property for Label.
               statusLabel.textProperty().unbind();

               // Bind the text property of Label
                // with message property of Task
               statusLabel.textProperty().bind(copyTask.messageProperty());

               // When completed tasks
               copyTask.addEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, //
                       new EventHandler<WorkerStateEvent>() {

                           @Override
                           public void handle(WorkerStateEvent t) {
                               List<File> copied = copyTask.getValue();
                               statusLabel.textProperty().unbind();
                               statusLabel.setText("Copied: " + copied.size());
                           }
                       });

               // Start the Task.
               new Thread(copyTask).start();

           }
       });

       // Cancel
       cancelButton.setOnAction(new EventHandler<ActionEvent>() {
           @Override
           public void handle(ActionEvent event) {
               startButton.setDisable(false);
               cancelButton.setDisable(true);
               copyTask.cancel(true);
               progressBar.progressProperty().unbind();
               progressIndicator.progressProperty().unbind();
               statusLabel.textProperty().unbind();
               //
               progressBar.setProgress(0);
               progressIndicator.setProgress(0);
           }
       });

       FlowPane root = new FlowPane();
       root.setPadding(new Insets(10));
       root.setHgap(10);

       root.getChildren().addAll(label, progressBar, progressIndicator, //
               statusLabel, startButton, cancelButton);

       Scene scene = new Scene(root, 500, 120, Color.WHITE);
       primaryStage.setTitle("ProgressBar & ProgressIndicator");
       primaryStage.setScene(scene);
       primaryStage.show();
   }

   public static void main(String[] args) {
       Application.launch(args);
   }

}
CopyTask.java
package org.o7planning.javafx.progress;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javafx.concurrent.Task;

// Copy all file in C:/Windows
public class CopyTask extends Task<List<File>> {

    @Override
    protected List<File> call() throws Exception {

        File dir = new File("C:/Windows");
        File[] files = dir.listFiles();
        int count = files.length;

        List<File> copied = new ArrayList<File>();
        int i = 0;
        for (File file : files) {
            if (file.isFile()) {
                this.copy(file);
                copied.add(file);
            }
            i++;
            this.updateProgress(i, count);
        }
        return copied;
    }

    private void copy(File file) throws Exception {
        this.updateMessage("Copying: " + file.getAbsolutePath());
        Thread.sleep(500);
    }

}
No ADS

JavaFX Tutorials

Show More
No ADS