2017-08-24 203 views
-1

我想在功能運行時顯示進度條。展示它的最佳方式是什麼?基本上我正在構建一個程序發送多個郵件在一個單一的點擊。發送郵件時,我想在發送郵件時顯示進度條。javafx:進度條顯示進程的進度?

+0

你需要的,如果你想要的任何像樣的答案提供了很多詳細信息。 – pancho018

+2

歡迎來到StackOverflow。我建議你花一些時間瀏覽本網站的[幫助頁面](https://stackoverflow.com/help),特別是關於[提問問題]的頁面(https://stackoverflow.com/help/問)。此論壇旨在提供*特定編程問題*的答案,通常最好的問題包含[MCVE]形式的代碼。在發佈之前,您應該研究您的問題,並創建一個示例來展示您嘗試解決問題的方法。你目前的問題實際上只是一個需求聲明,所以這是論壇的焦點話題。 –

+0

@ Rishabh-Sharma我的答案是否適合你? https://stackoverflow.com/a/45867364/8087490。如果有效,你可以接受我的答案爲正確 – Developer66

回答

1

這種情況下的最佳解決方案是使用Task

例子:

Task<Parent> yourTaskName = new Task<Parent>() { 
    @Override 
    public Parent call() { 
     // DO YOUR WORK 

     //method to set progress 
     updateProgress(workDone, max); 

     //method to set labeltext 
     updateMessage(message); 
    } 
}; 

//ProgressBar 
ProgressBar pBar = new ProgressBar(); 
//Load Value from Task 
pBar.progressProperty().bind(yourTaskName.progressProperty()); 
//New Loading Label 
Label statusLabel = new Label(); 
//Get Text 
statusLabel.setText("Loading..."); 
//Layout 
VBox root = new VBox(statusLabel, pBar); 
//SetFill Width TRUE 
root.setFillWidth(true); 
//Center Items 
root.setAlignment(Pos.CENTER); 

//SetOnSucceeded methode 
yourTaskName.setOnSucceeded(new EventHandler<WorkerStateEvent>() { 
     @Override 
     public void handle(WorkerStateEvent event) { 
      System.out.println("Finish"); 
     } 
}); 

//Start Thread 
Thread loadingThread = new Thread(yourTaskName); 
loadingThread.start(); 

希望這有助於你。

P.S:在運行爲Thread任務的代碼...

0

我實現你想要的最後一次,如果你想顯示progressIndicator或進度發送運行時,試試這部分代碼

senderThreadlive = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        try { 
         Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
           ProgressIndicator WaitingSend=new ProgressIndicator(); 
           WaitingSend.setProgress(ProgressIndicator.INDETERMINATE_PROGRESS); 
           WaitingBox.getChildren().add(WaitingSend);//this is an HBOX 
           SendMailButton.setDisable(true); 
           SendMailButton.setText("sending in progress"); 

           } 
         }); 
         //call Your method of sending 
SimpleMail.EmailSender.sendEmail(MailSenderTxt.getText(), MotMailTxt.getText(), DestMailTxt.getText(), ObjetMailTxt.getText(), org.jsoup.Jsoup.parse(ContentMail.getHtmlText()).text()); 

         Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
           WaitingSend.setProgress(0); 
           WaitingSend.setVisible(false); 
           SendMailButton.setDisable(false); 
           SendMailButton.setText("Send");          
          } 
         }); 

        } catch (AuthenticationFailedException e) { 

         Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
           //Your popUp here 
          } 
         }); 

        } catch (SendFailedException e) { 

         Platform.runLater(new Runnable() { 
          @Override 

          public void run() { 
           //Your popUp here 
          } 
         }); 

        } catch (MessagingException e) { 

         Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
          //Your popUp here 

          } 
         }); 

        } catch (Exception ex) { 

         Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
            //Your popUp here 

          } 
         }); 

        } 
       } 
      }); 
      senderThreadlive.start(); 
+0

代碼太麻煩了...有更簡單的方法 – Developer66