2013-02-14 81 views
2

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

我的按钮事件:

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

     } 
    } 

[EDITED]

与我的关于javafx的问题我已经注意到许多新开发人员正在面临管理线程的问题。我想分享我已经做了什么来简化我在javafx上管理线程的生活。我已经创建了一个基于Android的AsyncTask的AsyncTask类,它基本上以一种不起眼但有效的方式来执行Android的相同操作。你可以在Github project

回答

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