按下按钮时,ProgressIndicator冻结。 JavaFX的

时间:2013-02-14 15:17:28

标签: java javafx progress-indicator

我正在使用指标进度设置为-1.0来显示loginprocess正在运行时的一些加载。 但是,当我按下Enter按钮并使用loginProcess启动执行程序时,即使我使用freezed设置可见Plataform.runLater,我的界面也会保持ProgressIndicator

我的按钮事件:

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

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

方法showLoginLoading:

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

            Platform.runLater(new Runnable() {

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

        }
    }

1 个答案:

答案 0 :(得分:0)

问题在于线程管理。我试图在主FX视图运行的同一个线程中执行登录指令。 我用Platform.isFxApplicationThread()来计算它;如果调用线程是JavaFX应用程序线程,则返回true。

要解决我的问题,我只需创建一个新线程来运行我的所有登录说明,如下图所示:

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