How to show progress with JavaFX ProgressBar with background running process?

In this blog, you will learn how to show progress in progress bar from code if you are running any background process and that is updating progress on progress bar control. Please follow the steps to implement this concept in your JavaFX application.

Step 1: First, create a Simple Fxml based JavaFX project in your Netbeans IDE.Now open FXML from your project and open inside scene builder and drag and drop progress bar in FXML like given image and add one button to initiate progress.

 
 
Step 2:  Once you have done with FXML then you have to add the following code to show progress in progress bar from background running process.
 
FXMLDocument.fxml code:
<?xml version=”1.0″ encoding=”UTF-8″?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane id=”AnchorPane” prefHeight=”303.0″ prefWidth=”355.0″ xmlns=”http://javafx.com/javafx/8.0.141″ xmlns:fx=”http://javafx.com/fxml/1″ fx:controller=”javafxprogressbar.FXMLDocumentController”>
    <children>
        <Button fx:id=”button” layoutX=”129.0″ layoutY=”173.0″ onAction=”#handleButtonAction” text=”Start Progress” />
        <Label fx:id=”label” layoutX=”23.0″ layoutY=”135.0″ minHeight=”16″ minWidth=”65.0″ prefHeight=”17.0″ prefWidth=”224.0″ />
      <ProgressBar fx:id=”progressBar” layoutX=”23.0″ layoutY=”111.0″ prefHeight=”18.0″ prefWidth=”305.0″ progress=”0.0″ />
      <Label layoutX=”23.0″ layoutY=”78.0″ text=”Progress Bar with Background Process”>
         <font>
            <Font name=”System Bold” size=”16.0″ />
         </font>
      </Label>
    </children>
</AnchorPane>
FXMLDocumentController.java file code:
package javafxprogressbar;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
/**
 *
 * @author Cool IT Help
 */
public class FXMLDocumentController implements Initializable {   
    @FXML
    private Label label;   
    @FXML
    private ProgressBar progressBar;   
    Task copyWorker;   
    @FXML
    private void handleButtonAction(ActionEvent event) {  
        progressBar.setProgress(0.0);
        copyWorker = createWorker();       
        progressBar.progressProperty().unbind();       
        progressBar.progressProperty().bind(copyWorker.progressProperty());       
        copyWorker.messageProperty().addListener(new ChangeListener<String>() {
       public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                        System.out.println(newValue);
                        label.setText(newValue);
                    }
                });               
        new Thread(copyWorker).start();               
    }
   
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }       
    public Task createWorker() {
        return new Task() {
            @Override
            protected Object call() throws Exception {
                for (int i = 0; i < 10; i++) {
                    Thread.sleep(2000);
                    updateMessage(“Task Completed : ” + ((i*10)+10)  + “%”);
                    updateProgress(i + 1, 10);
                }
                return true;
            }
        };
    }
}
 
JavaFXProgressBar.java file code.
package javafxprogressbar;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
 *
 * @author Cool IT Help
 */
public class JavaFXProgressBar extends Application {   
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource(“FXMLDocument.fxml”));
        Scene scene = new Scene(root);       
        stage.setScene(scene);
        stage.show();
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }   
}
 
Once you ready and do a clean build and run your project and you will see following screens once you pressStart Progressbutton.
                                          Progress Bar Screen 1:
 
 
Progress Bar Screen 2
Progress Bar Screen 3
 
If you guys still having issues so please follow this link to download the sample project:
 
Download link: 
https://drive.google.com/open?id=11hX-Cce1uOlcEJOQHt-q8KYIq0AuDSiY
 

Leave a Comment