2013-02-14 102 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(); 
      }); 
     }