How to create a ProgressIndicator in JavaFX?


A progress indicator is a circular UI component which is used to indicate the progress of certain action. You can create a progress indicator by instantiating the javafx.scene.control.ProgressIndicator class.

Example

The following Example demonstrates the creation of a ProgressIndicator.

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ProgressIndicatorExample extends Application {
   public void start(Stage stage) {
      //Creating a progress indicator
      ProgressIndicator indicator = new ProgressIndicator(0.6);
      //Setting the size of the progress bar
      indicator.setPrefSize(300, 120);
      //Creating a slider
      Slider slider= new Slider(0, 1, 0.2);
      slider.setShowTickLabels(true);
      slider.setShowTickMarks(true);
      slider.setMajorTickUnit(0.25);
      slider.setBlockIncrement(0.1);
      slider.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){
            //Setting the angle for the rotation
            indicator.setProgress((double) newValue);
            //Setting value to the indicator
            indicator.setProgress((double) newValue);
         }
      });
      //Creating a vbox to hold the progress bar and progress indicator
      VBox vbox = new VBox(20);
      vbox.setSpacing(5);
      vbox.setPadding(new Insets(15, 150, 10, 60));
      vbox.getChildren().addAll(slider, indicator);
      vbox.setStyle("-fx-background-color: BEIGE");
      //Setting the stage
      Scene scene = new Scene(vbox, 595, 200, Color.BEIGE);
      stage.setTitle("Progress Indicator");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

Output

Updated on: 20-May-2020

254 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements