JavaFX - ProgressIndicator



A progress indicator is a UI control that is used to indicate the progress of a task to the user. It is often used with the Task API to notify users about the progress of background tasks and how much time will be required to complete the user action. In this tutorial, we are going to learn how to create and use progress indicator in JavaFX. Before that, let's see how a general progress indicator looks like −

Sample progress indicator

ProgressIndicator in JavaFX

In JavaFX, the progress indicator is represented by a class named ProgressIndicator. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a progress indicator in JavaFX. Constructors of the ProgressIndicator class are listed below −

  • ProgressIndicator() − It constructs a new progress indicator without initial progress value.

  • ProgressIndicator(double progress) − It constructs a new progress indicator with a specified initial progress value.

Example

The following program demonstrates how to create a progress indicator in JavaFX. Save this code in a file with the name ProgressindicatorJavafx.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.stage.Stage;
public class ProgressindicatorJavafx extends Application {
   @Override
   public void start(Stage stage) {
      // Creating a progress indicator without an initial progress value
      ProgressIndicator progress = new ProgressIndicator();
      // Create a scene and stage
      Scene scene = new Scene(progress, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Progress Indicator in Javafx");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ProgressindicatorJavafx.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ProgressindicatorJavafx

Output

When we execute the above code, it will generate the following output.

ProgressIndicator Output

ProgressIndicator with an Initial Progress Value

We can set the initial progress value to the progress indicator by using the parameterized constructor of the ProgressIndicator class. The constructor accepts a progress value as an argument which is of double type ranging between 0.0 and 1.0. Here, 0.0 means no progress and 1.0 means complete. If the progress value is negative, the progress indicator is indeterminate, meaning that it does not show any specific progress amount.

Example

In the following example, we are creating a progress indicator initialized with a specific progress value. Additionally, we are also creating two buttons labelled 'Increase' and 'Decrease'. Clicking the 'Increase' button increments the progress value, while clicking 'Decrease' decrements it. Save this code in a file with the name Javafxprogress.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Javafxprogress extends Application {
   @Override
   public void start(Stage stage) {
      // Create a progress indicator with an initial progress of 0.5
      ProgressIndicator progressIndctr = new ProgressIndicator(0.5);
      // Create a button that increases the progress by 0.1
      Button buttonOne = new Button("Increase");
      buttonOne.setOnAction(e -> {
         // Get the current progress value
         double progress = progressIndctr.getProgress();
         // Increase the progress by 0.1
         progress += 0.1;
         // Set the new progress value
         progressIndctr.setProgress(progress);
      });
      // Create second button that decreases the progress by 0.1
      Button buttonTwo = new Button("Decrease");
      buttonTwo.setOnAction(e -> {
         // Get the current progress value
         double progress = progressIndctr.getProgress();
         // Decrease the progress by 0.1
         progress -= 0.1;
         // Set the new progress value
         progressIndctr.setProgress(progress);
      });
      // Create an HBox to hold the progress indicator and the button
      HBox hbox = new HBox(10);
      hbox.getChildren().addAll(progressIndctr,buttonOne, buttonTwo);
      // Create a scene with the HBox and set it to the stage
      Scene scene = new Scene(hbox, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Progress Indicator in JavaFX");
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt by using the following commands −

javac --module-path %PATH_TO_FX% --add-modules javafx.controls Javafxprogress.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls Javafxprogress

Output

On executing the above code, it will generate the following output.

ProgressIndicator Output2
Advertisements