【问题标题】:ProgressIndicator freezes when button is pressed. JavaFX按下按钮时,ProgressIndicator 会冻结。 JavaFX
【发布时间】:2013-01-30 10:39:31
【问题描述】:

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

我的按钮事件:

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

我的登录过程:

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

        }
    }

【问题讨论】:

    标签: java javafx progress-indicator


    【解决方案1】:

    问题出在线程管理中。我试图在主 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();
               });
            }
    

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 2021-06-24
      • 2020-02-14
      • 1970-01-01
      相关资源
      最近更新 更多