2017-08-24 79 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