Progressbar loading per Javafx Process time

Asked

Viewed 1,116 times

2

I have a method that carries a ProgressBar, but it only starts loading at the end of the process, that is after my application finishes generating the reports it starts to be filled.

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

This is the way:

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;
}

2 answers

3


Need to add bind between component progress properties with the service.

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

Here a complete working example.

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();
    }
}

0

For people who are working in parallel with 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();
            }
        });
   }     

Browser other questions tagged

You are not signed in. Login or sign up in order to post.