ProgressBar loading according to JavaFX process time

2

I have a method that loads a ProgressBar , but it only starts loading at the end of the process, ie after my application finishes generating the reports it starts to fill.

How do I load it according to the process time that generates the reports?

This is the method:

private Service getTask() {
        Service<Void> servico = new Service() {
            @Override
            protected Task createTask() {
                return new Task() {
                    @Override
                    protected Void call() throws Exception {
                        updateMessage("Carregando...");
                        Thread.sleep(200);
                        //nRel is my accountant
                        updateProgress(1, nRel);
                        for (int i = 0; i < nRel; i++) {
                            updateProgress(i + 1, nRel);
                            updateMessage("Carregando " + (i + 1) + " de " + nRel);
                            Thread.sleep(400);
                        }
                        updateMessage("Processo Finalizado");
                        return null;
                    }
                };
            }
        };

        return servico;
}
    
asked by anonymous 28.05.2015 / 13:35

2 answers

2

You need to add bind between the component's progress properties with the service.

progressBar.progressProperty().bind(service.progressProperty());

Here's a full example working.

import javafx.application.Application;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.concurrent.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class TaskTest extends Application {

    public static void main(String[] args) throws Exception {
        launch(args);
    }

    public void start(final Stage stage) throws Exception {
        final Label statusLabel = new Label("Status");
        final Button runButton = new Button("Run");
        final ListView<String> peopleView = new ListView<String>();
        peopleView.setPrefSize(220, 162);
        final ProgressBar progressBar = new ProgressBar();
        progressBar.prefWidthProperty().bind(peopleView.prefWidthProperty());

        runButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Service<ObservableList<String>> service = new Service() {
                    @Override
                    protected Task createTask() {
                        return new Task() {
                            @Override
                            protected ObservableList<String> call() throws Exception {
                                updateMessage("Finding friends . . .");
                                for (int i = 0; i < 10; i++) {
                                    Thread.sleep(200);
                                    updateProgress(i + 1, 10);
                                }
                                updateMessage("Finished.");
                                return FXCollections.observableArrayList("John", "Jim", "Geoff", "Jill", "Suki");
                            }
                        };
                    }
                };
                statusLabel.textProperty().bind(service.messageProperty());
                runButton.disableProperty().bind(service.runningProperty());
                peopleView.itemsProperty().bind(service.valueProperty());
                progressBar.progressProperty().bind(service.progressProperty());
                service.stateProperty().addListener(new ChangeListener<Worker.State>() {
                    @Override
                    public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState, Worker.State newState) {
                        if (newState == Worker.State.SUCCEEDED) {
                            System.out.println("This is ok, this thread " + Thread.currentThread() + " is the JavaFX Application thread.");
                            runButton.setText("Voila!");
                        }
                    }
                });

                service.start();
            }
        });

        final VBox layout
                = VBoxBuilder.create().spacing(8).children(
                        VBoxBuilder.create().spacing(5).children(
                                HBoxBuilder.create().spacing(10).children(
                                        runButton,
                                        statusLabel).build(),
                                progressBar
                        ).build(),
                        peopleView
                ).build();
        layout.setStyle("-fx-background-color: cornsilk; -fx-padding:10; -fx-font-size: 16;");
        Scene scene = new Scene(layout);
        stage.setScene(scene);
        stage.show();
    }
}
    
23.08.2015 / 08:32
0

For people who are working in parallel with the Scenebuilder (Using FXML), the method to reproduce this action above would be:

public void Teste (Action event) {

runButton.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(ActionEvent actionEvent) {
                Service<ObservableList<String>> service = new Service() {
                    @Override
                    protected Task createTask() {
                        return new Task() {
                            @Override
                            protected ObservableList<String> call() throws Exception {
                                updateMessage("Finding friends . . .");
                                for (int i = 0; i < 10; i++) {
                                    Thread.sleep(200);
                                    updateProgress(i + 1, 10);
                                }
                                updateMessage("Finished.");
                                return FXCollections.observableArrayList("John", "Jim", "Geoff", "Jill", "Suki");
                            }
                        };
                    }
                };
                statusLabel.textProperty().bind(service.messageProperty());
                runButton.disableProperty().bind(service.runningProperty());
                peopleView.itemsProperty().bind(service.valueProperty());
                progressBar.progressProperty().bind(service.progressProperty());
                service.stateProperty().addListener(new ChangeListener<Worker.State>() {
                    @Override
                    public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State oldState, Worker.State newState) {
                        if (newState == Worker.State.SUCCEEDED) {
                            System.out.println("This is ok, this thread " + Thread.currentThread() + " is the JavaFX Application thread.");
                            runButton.setText("Voila!");
                        }
                    }
                });

                service.start();
            }
        });
   }     
    
20.06.2017 / 14:53