Question

I'm using a indicator progress set to -1.0 to show some loading while loginprocess is running. But when I press the Enter button and start my executor with the loginProcess, my interface stays freezed even if I use Plataform.runLater to set visible my ProgressIndicator.

My button event:

public void initManager(final LoginManager loginManager) {
    btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {


            String email = loginTxtField.getText().trim();
            String token = tokenTxtField.getText().trim();

            if (email.equals("")) {
                Platform.runLater(new Runnable() {
                    public void run() {
                        Dialog.showError("Erro", "Digite o e-mail");
                    }
                });
                return;
            }

            try {

                Future future = loginProcess(email, token);

                showLoginLoading(future);

                future.get();

                if (!loginGatewayFailed && !loginTargetAppFailed) {

                    Login loginTargetApp = new Login(email, null, null);
                    loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

                } else {

                    if (loginTargetAppFailed) {

                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", loginTargetAppFailedCause);
                            }
                        });

                    } else {

                        if (loginGatewayFailed) {

                            Platform.runLater(new Runnable() {
                                public void run() {
                                    Dialog.showError("Erro", loginGatewayFailedCause);
                                }
                            });
                        }
                    }
                }

            } catch (final Exception ex) {

                Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

                Platform.runLater(new Runnable() {
                    public void run() {
                        Dialog.showError("Erro", ex.getMessage());
                    }
                });
            }
        }
    });
}

My loginProcess:

public Future<?> loginProcess(String email, String token) throws Exception {

// MY PROCESS

            return Executors.newSingleThreadExecutor().submit(new LoginTask(this, email, token));

        } catch (Exception e) {

            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, e.getMessage());

            throw e;
        }
    }

method showLoginLoading:

private void showLoginLoading(Future future) {
        while (!future.isDone()) {

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    progressInd.setVisible(true);
                   // progressInd.setProgress(-1.0);
                }
            });

        }
    }
Was it helpful?

Solution

The problem was in thread management. I was trying to execute the login instructions in the same thread that the main FX view runs. I figured it out using the Platform.isFxApplicationThread(); It returns true if the calling thread is the JavaFX Application Thread.

To fix my problem i just needed to create a new thread to run all my login instructions as you can see in bellow example:

public void initManager(final LoginManager loginManager) {
    btnEntrar.setOnAction(new EventHandler<ActionEvent>() {
    boolean mainThread = Platform.isFxApplicationThread();
            System.out.println("This is the main Thread: " + mainThread);

            Platform.runLater(new Runnable() {

                @Override
                public void run() {
                    progressInd.setVisible(true);
                }
            });

            new Thread() {
                public void run() {

                    boolean mainThread = Platform.isFxApplicationThread();
                    System.out.println("This is the main Thread: " + mainThread);

                    String email = loginTxtField.getText().trim();
                    String token = tokenTxtField.getText().trim();

                    if (email.equals("")) {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", "Digite o e-mail");
                            }
                        });
                        return;
                    }

                    try {

                        Future future = loginProcess(email, token);

            //            showLoginLoading(future);

                        future.get();

                        if (!loginGatewayFailed && !loginTargetAppFailed) {

                            Login loginTargetApp = new Login(email, null, null);
                            loginManager.autheticated(loginTargetApp, loginGateway, gateway, file);

                        } else {

                            if (loginTargetAppFailed) {

                                Platform.runLater(new Runnable() {
                                    public void run() {
                                        Dialog.showError("Erro", loginTargetAppFailedCause);
                                    }
                                });

                            } else {

                                if (loginGatewayFailed) {

                                    Platform.runLater(new Runnable() {
                                        public void run() {
                                            Dialog.showError("Erro", loginGatewayFailedCause);
                                        }
                                    });
                                }
                            }
                        }

                    } catch (final Exception ex) {

                        Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, ex.getMessage());

                        Platform.runLater(new Runnable() {
                            public void run() {
                                Dialog.showError("Erro", ex.getMessage());
                            }
                        });
                    }


                }
            }.start();
           });
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top