ProgressBar
ProgressBar in JavaFX is a control that visually represents the progress of a task. It ranges from 0.0 (0%) to 1.0 (100%), and can also be indeterminate when the exact progress isn’t known.
Example: Simple Progress Bar
FXML (hello-view.fxml)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.ProgressBar?>
<?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">
<ProgressBar fx:id="progressBar" prefWidth="200"/>
<Button text="Start Task" onAction="#startTask"/>
</VBox>
Controller (HelloController.java)
package org.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
import javafx.concurrent.Task;
public class HelloController {
@FXML
private ProgressBar progressBar;
@FXML
private void startTask() {
// Create a background task
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;
}
};
// Bind ProgressBar to Task progress
progressBar.progressProperty().bind(task.progressProperty());
// Run task in a background thread
new Thread(task).start();
}
}
How It Works
progressProperty()is bindable, so it can reflect the task’s progress automatically.ProgressBarvalue ranges:0.0→ 0%1.0→ 100%-1→ indeterminate mode (moving stripes)
Using
Taskallows running the progress updates in a background thread, so the UI doesn’t freeze.
💡 Tip: You can also use ProgressIndicator if you want a circular progress display instead of a horizontal bar.