将JProgressBar与Java Mail结合使用(知道transport.send()之后的进度) [英] Using JProgressBar with Java Mail ( knowing the progress after transport.send() )

查看:141
本文介绍了将JProgressBar与Java Mail结合使用(知道transport.send()之后的进度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有发送电子邮件的程序.我想知道是否可以使用progress bar来改善用户界面.我想要的是进度条应该在遇到transport.send()语句后相应地进行进度.有没有办法我可以知道进度. 知道的是,当用户按下send时,启动了一个新线程来发送电子邮件.单击send之后的响应很差,因为用户不知道自己的行为是否正在被监听.半分钟后,他得到一个JOptionPane,是,消息已发送.您将同意每个人都渴望知道他的操作是否正在处理./p>

我是否可以使用进度条.(即我如何知道电子邮件程序的进度) 如果我不能使用JProgressBar,我还可以使用另一种方法来告诉用户他的命令正在处理,而他不必担心.

顺便说一下,这是负责发送电子邮件的部分.

try {
           message.setFrom( new InternetAddress(from));
           message.setRecipients(MimeMessage.RecipientType.TO , InternetAddress.parse(to) );
           message.setSubject(subject);
           message.setText(emailMessage);
           attachment.setDataHandler( new DataHandler( fds ) );
           attachment.setFileName( fileName );
           messagePart.setText( emailMessage );
           Multipart gmailMP = new MimeMultipart();
           gmailMP.addBodyPart(attachment);
           gmailMP.addBodyPart( messagePart );
           message.setContent( gmailMP );
           Transport transport = session.getTransport("smtp");
           transport.send(message); // LINE THAT SENDS EMAIL
           transport.close();   

           JOptionPane.showMessageDialog(new JFrame() , "Message sent!");
       }    catch(Exception exc) {
               JOptionPane.showMessageDialog( new JFrame() , exc );
            }

在注释行之后,接下来的2条语句要花一些时间.在这之间,我希望用户知道他的动作正在处理

我该怎么做?

解决方案

如果我理解您的提问正确,那么您希望获得有关电子邮件发送过程的最新信息.在这种情况下,您可以通过进度条的形式向用户显示此信息.

据我所知,您在这里没有清晰"的解决方案.尽管javax.mail.Transport具有方法addTransportListener(TransportListener l),但接口TransportListener不会报告已完成工作的百分比.坦白地说,我不确定是否有可能.您可以做的是在流程开始和结束时回电.您可以在程序中添加逻辑,以倾斜"发送电子邮件通常需要多长时间,然后尝试使用计时器任务模仿"进度.例如,如果通常需要30秒,则每1秒将3%添加到进度栏中.然后停止,除非电子邮件发送完成.如果发送完成较快,则立即跳到100%.您的程序可以进行倾斜和更新,如果网络变得更快,它将估计为20秒而不是30秒.

我认为不存在更好的解决方案.不用担心:世界上大多数过程条都是基于某种估计,启发式方法等.

I have this program that sends email.I was wondering if i could use progress bar to make user interface better. What i want is that the progress bar should progress accordingly after the statement transport.send() is encountered.Is there a way i can know the progress . What happens know is , as the user presses send a new thread is started that sends the email.The response after clicking send is poor as the user does not know that his action is being listened or not.(though it is being listened !) After a gap of half a minute he gets a JOptionPane that yes , the message has been sent.You will agree that everyone is eager to know that his action is being processed or not.

Is there any way out i can use progress bar.(i.e how can i know the progress of my email program) If i can't use JProgressBar what is the other method i can use to tell the user that his command is being processed and he need not worry.

By the way this is the part responsible for sending emails.

try {
           message.setFrom( new InternetAddress(from));
           message.setRecipients(MimeMessage.RecipientType.TO , InternetAddress.parse(to) );
           message.setSubject(subject);
           message.setText(emailMessage);
           attachment.setDataHandler( new DataHandler( fds ) );
           attachment.setFileName( fileName );
           messagePart.setText( emailMessage );
           Multipart gmailMP = new MimeMultipart();
           gmailMP.addBodyPart(attachment);
           gmailMP.addBodyPart( messagePart );
           message.setContent( gmailMP );
           Transport transport = session.getTransport("smtp");
           transport.send(message); // LINE THAT SENDS EMAIL
           transport.close();   

           JOptionPane.showMessageDialog(new JFrame() , "Message sent!");
       }    catch(Exception exc) {
               JOptionPane.showMessageDialog( new JFrame() , exc );
            }

After the commented line it takes some time for the next 2 statements to execute.In between i want the user to know that his action is being processed

How can i do that ?

解决方案

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.

这篇关于将JProgressBar与Java Mail结合使用(知道transport.send()之后的进度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆