javafx:显示流程进度的进度条?

时间:2017-08-24 14:02:08

标签: javafx

我想在功能运行时显示进度条。展示它的最佳方式是什么?基本上我正在构建一个程序,只需单击即可发送多封邮件。在发送邮件时,我想在发送邮件时显示进度条。

3 个答案:

答案 0 :(得分:4)

在这种情况下,最佳解决方案是使用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 ...

运行

答案 1 :(得分:0)

只需使用javafx.scene.control.ProgressBar

即可

文档: http://docs.oracle.com/javafx/2/ui_controls/progress.htm

答案 2 :(得分:0)

我上次实现了你想要的,如果你想在发送运行时显示progressIndicator或progressBar,试试这部分代码

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