Javafx - Update progress indicator in UI from java class

191 Views Asked by At

The following are the changes I made in fxml

Changes in the java file , here my code :

private ProgressIndicator pi;

void handlebuildButtonAction(ActionEvent event) throws IOException, GeneralSecurityException {

if ((entServer.isSelected()==true || compasServer.isSelected()==true)) {

            if(!fileList.isEmpty()){
                ProgressIndicator pi = new ProgressIndicator();
                pi.setProgress(10);
}
}

The progress indicator is not updated when I run the application. I'm not sure how to sync the changes to UI. Assist me on this. Thanks in advance.

output

1

There are 1 best solutions below

8
William On

For example: if you set 0.1 - progress will be 10%, 0.2 - 20% and so on, so when you set the progress => 1 you will always have "done".

Here, this an example with a button, when you click the button, your progress indicator will be updated(one click + 10%):

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;

public class Test extends Application {
    private ProgressIndicator pi;
    private double counter = 0;
    public void start(Stage stage)
    {
        ProgressIndicator pi = new ProgressIndicator();
        Button button = new Button("Press");

        TilePane root = new TilePane();

        // action event
        EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                counter += 0.1;
                pi.setProgress(counter);
            }
        };


        button.setOnAction(event);

        root.getChildren().add(button);
        root.getChildren().add(pi);

        // create a scene
        Scene scene = new Scene(root, 200, 200);

        // set the scene
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String args[])
    {
        // launch the application
        launch(args);
    }
}

Just change this code for your case:

EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e)
            {
                if ((entServer.isSelected()==true || compasServer.isSelected()==true)) {

                    if (!fileList.isEmpty()) {
                         counter += 0.1;
                         pi.setProgress(counter);
                    }
                }
            }
        };

Hope that helps you!