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 c开发者_运维百科ommand 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.
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() );
}
}
精彩评论