Using JProgressBar with Java Mail ( knowing the progress after transport.send() )

♀尐吖头ヾ 提交于 2019-12-03 21:53:26

If I understand you question correctly you wish to get updated about the process of email sending. In this case you can show this information to user in form of progress bar.

As far as I know you do not have "clear" solution here. Although javax.mail.Transport has method addTransportListener(TransportListener l) the interface TransportListener does not report what is the percentage of work is already done. Frankly speaking I am not sure it is possible. What you can do is to get call back when process is started and ended. You can add a logic to your program that "leans" how long does typically take to send email and then try to "mimic" the progress using timer task. For example if typically it takes 30 seconds add 3% to your progress bar every 1 second. Then stop unless the email sending is completed. If sending is completed faster jump to 100% immediately. You program can lean and update itself, is if network becomes faster it will estimate the time as 20 seconds instead of 30 etc.

I do not think that better solution exists. And do not worry: most process bars in the world are based on some kind of estimations, heuristics etc.

I appreciate the method by @ AlexR .

This is how i implemented it . It is near to perfect as far as user satisfaction on knowing the button's response is concerned.(but at times may cause problem when the connection is too slow)

boolean status = false;  // global variable
// add the following statement just before the statement transport.send()
startProgressThread(); // start a separate thread for progress bar
transport.send();  // progress bar started just before this statement is encountered
// the execution of the following statement will take some time,by the time JProgressBar is working
status = false; // as this statement is encountered while loop starts and jprogress bar stops.

public void startProgressThread() {
    // start the thread and from here call the function showProgress()
}

public showProgress() {
     status = true;
     int i = 0;
        while( status ) {
           i++;
           jProgressBar.setValue(i);
              try {
                Thread.sleep(90);
              }  catch(Exception exc) {
                     System.out.println(exc);
                 }
        }
     if( status != true ) {
         jprogressBar.setValue( jProgressBar.getMaximum() );
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!