
Don’t Block the GUI #92
Chapter 12, Miscellany
|
459
HACK
SwingUtilities.invokeLater(new Runnable( ) {
public void run( ) {
//label.setText("You have " + new_count + " unread messages.");
if(new_count > 0) {
label.setFont(new Font("WebDings",
Font.PLAIN,40));//label.getFont().getSize( )));
label.setText(""+(char)0xf099);
} else {
label.setFont(new Font("WingDings",
Font.PLAIN,40));//label.getFont().getSize( )));
label.setText(""+(char)0x2709);
}
System.out.println("unread messages = " + new_count);
}
});
This code will set the label to character 0xf099 in the WebDings font (the
envelope with a lightning bolt) if there is at least one unread email. If there
are no unread emails, it will use character
0x2709 in the WingDings font (a
plain envelope).
Java doesn’t support all of the glyphs in certain custom
fonts, so be sure to test your applications before going into
production whenever you use non-ASCII fonts.
H A C K
#92
Don’t Block the GUI Hack #92
Thread your heavy lifting so the event-dispatch thread stays responsive.
Practically every AWT and Swing book you’ll ever see keeps things simple
by responding to button clicks, menu selections, and other actions by doing
something in the event listener. That’s probably good for helping you learn
the various GUI widgets, but it sets you up for a really bad habit: putting
increasingly long-lasting calls in your event callbacks.
This is bad because the thread ...