JavaFX ProgressBar Example

In this JavaFX source code example, we will see how to use the JavaFX ProgressBar control with an example.

ProgressBar is a control that indicates the processing of a particular task with a completion bar.

JavaFX ProgressBar Example

The example consists of a progress bar and a button. The button starts the progress bar that is animated for a few seconds.

package sample;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    public void start(Stage stage) {


    private void initUI(Stage stage) {

        var root = new HBox(15);
        root.setPadding(new Insets(10));

        var pbar = new ProgressBar(0);

        var frame1 = new KeyFrame(Duration.ZERO,
                new KeyValue(pbar.progressProperty(), 0));

        var frame2 = new KeyFrame(Duration.seconds(15),
                new KeyValue(pbar.progressProperty(), 1));

        var task = new Timeline(frame1, frame2);

        var btn = new Button("Start");
        btn.setOnAction((ActionEvent actionEvent) -> task.playFromStart());

        root.getChildren().addAll(pbar, btn);

        var scene = new Scene(root);


    public static void main(String[] args) {

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course