454
|
Chapter 12, Miscellany
#91 Check Your Mail with Swing
HACK
moveMouse(delete, delete, 1000);
moveMouse(delete, showme, 500);
} catch (Exception ex) {
System.out.println(""+ex);
}
}
The event handler moves the cursor from the
showme
button, which is where
the cursor will already be anyway, to the
info toolbar button over a period
of two seconds. After that, the cursor will hop from button to button taking
one second to move with a one second pause over each button. Finally, the
cursor rushes back to the
showme button in a half-second. It is important to
return the cursor to its starting position so that the user won’t have to hunt
for it after the animation is complete. It’s best to always return things to the
state you found them.
H A C K
#91
Check Your Mail with Swing Hack #91
Add email checking to your application with just a few method calls.
As email becomes a bigger part of our daily lives, I have seen it creep into
more and more places. My email program alerts me when there is new mail.
I can check my email via the phone. I log in to my web mail from an Inter-
net cafe. Email is everywhere, so why shouldn’t it be in your Swing applica-
tion? This hack shows how to embed in your application an email checker
that shows the current number of unread messages and can launch the
user’s email application.
Dealing with email servers can be a complicated and tricky business. To
help address these issues, Sun created the JavaMail API, which is a set of
classes defining a vendor-neutral interface for accessing email servers.
Sun’s sample implementation provides IMAP support, which
is what I will demonstrate here. If you have another kind of
email server, such as Exchange, you could install your own
service provider and use it the same way.
The code in this hack needs to do two things. First, it must open a connec-
tion to the email server periodically and check for new mail. Second, it must
launch the user’s email program on a double-click. I have encapsulated the
email checking and launching code into separate classes, making it very easy
to add to an existing program.
Check Your Mail with Swing #91
Chapter 12, Miscellany
|
455
HACK
The EmailChecker class, shown in Example 12-5, is a simple Runnable imple-
mentation that receives a
JLabel to its constructor. The run loop will sleep
for a certain amount of time (one minute in this case) then call
checkEmail( )
.
Every time there is new mail, it will set the text of the label to something like
“You have
N
new messages.”
Next comes the
checkEmail( ) implementation:
public synchronized void checkEmail( ) throws Exception {
String username = "joshy@code.joshy.org";
String password = "satans";
String hostname = "code.joshy.org";
int port = 143;
Properties props = System.getProperties( );
Session sess = Session.getDefaultInstance(props);
sess.setDebug(true);
To actually check the email, you need to first collect the relevant parame-
ters: the username, password, hostname of the email server, and the port
(usually 143 for IMAP servers). In addition to this information, you also
need a copy of the system properties to allocate an email
Session.
Example 12-5. Checking for new messages
import java.util.Properties;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.mail.*;
public class EmailChecker implements Runnable {
private JLabel label;
public EmailChecker(JLabel label) {
this.label = label;
}
public void run( ) {
while(true) {
try {
checkEmail( );
Thread.currentThread( ).sleep(1000*60); // sleep 1 min
} catch (Exception ex) {
System.out.println("exception: " + ex);
ex.printStackTrace( );
}
}
}

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.