Progress Indicator
ProgressIndicator in JavaFX is similar to ProgressBar, but it shows progress circularly instead of horizontally. It also ranges from 0.0 (0%) to 1.0 (100%) and can be indeterminate (spinning) when progress isnβt known.
Example: Simple Circular Progress Indicator
FXML (hello-view.fxml)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ProgressIndicator?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>
<VBox spacing="10" alignment="CENTER"
xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.example.demo.HelloController">
<ProgressIndicator fx:id="progressIndicator" prefWidth="100" prefHeight="100"/>
<Button text="Start Task" onAction="#startTask"/>
</VBox>
Controller (HelloController.java)
package org.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressIndicator;
import javafx.concurrent.Task;
public class HelloController {
@FXML
private ProgressIndicator progressIndicator;
@FXML
private void startTask() {
Task<Void> task = new Task<>() {
@Override
protected Void call() throws Exception {
for (int i = 1; i <= 100; i++) {
Thread.sleep(50); // simulate work
updateProgress(i, 100); // update progress
}
return null;
}
};
progressIndicator.progressProperty().bind(task.progressProperty());
new Thread(task).start();
}
}
Key Notes:
Progress Value
0.0β 0%1.0β 100%-1β indeterminate (spinning animation)
Binding
progressIndicator.progressProperty().bind(task.progressProperty());keeps the circular indicator synced with the task.
Indeterminate Mode Example
progressIndicator.setProgress(-1); // starts spinning
π‘ Tip: ProgressIndicator is often used for loading dialogs or tasks where a circular spinner looks cleaner than a horizontal bar.