Java Tips

References: http://java.sun.com/developer/Books/gui/swinghacks/

Because I've authored a book about Java Foundation Classes/Swing (JFC/Swing), people often think that I must know everything about JFC/Swing. Of course, I don't. Swing is an enormous API, and the encyclopedic book that I coauthored in 1998 leaves no stone unturned in the world of components that start with J.
The good news is that, with such a large book, I can often deny having intimate knowledge of a particular API and blindly say something like, "My coauthor wrote about that component!" -- especially if I've forgotten how to use it. The bad news is that I often find myself wishing for a deeper level of experience with all of those components, only a third of which I wrote about.
No sweat. With a little help from Swing Hacks: Tips and Tools for Building Killer GUIs, I can once again fool the world into thinking that I know everything there is to know about Swing. The book starts off with a bang, helping you to create snazzy-looking components by overriding the paintComponent() method. And it doesn't stop there.
About the Book
If you were at the 2005 JavaOne Conference, you may have dropped by two technical sessions on the topic: TS-3605: Extreme GUI Makeover and TS-3214: Advanced Java 2D API Topics for the Java Desktop. Both gave sound advice on making your Swing components look much better. This tip uses the same technique, overriding paintComponent(Graphics g), casting that incoming Graphics object to a Graphics2D, then allowing you to re-render to your heart's content. In fact, Sun's Romain Guy gave a portion of that first talk, and the Swing Hacks authors credit him with a few fantastic hacks in their book. If you're really feeling ambitious, Tip 12 helps you to create translucent windows, although you should be at least somewhat familiar with the pluggable look-and-feel capabilities of Swing first.
Chapter 6, "Transparent and Animated Windows," is one of my favorites because it continues this theme and helps my Swing components look a little more Mac-like. Creating transparent windows, creating frame-anchored sheets for dialogs, animating the sheet dialog, and sliding notes out from the taskbar are some of the hacks in that chapter. All it takes is a little knowledge of the Swing heavyweight component glass pane, and you're up and running.
Buried in Hack 54 is an invaluable gem: Want to antialias all the text on your Swing application without touching any code? No problem, just add the following definition to the command line when you invoke your application: java -Dswing.aatext=true MyStartClass
Chapter 10, "Audio," is also a good chapter to look at, because many Swing programmers tend to overlook sound as an important part of their application. Maybe you want the swishing sound of a folder closing or of a clanging trash can when you throw away something in your Swing application. Hacks 70 through 73 discuss playing sounds with applets, JavaSound, the Java Media Framework, and Quicktime for Java technologies. Hack 74 shows you how to add MP3 support to the Java Media Framework API as well. (Is that a Swing hack? Not really. But it's pretty simple to execute, and I'm glad it's there.)
Chapter 12, "Miscellany," presents us with some obvious tricks and some very important tips. For example, most programmers know that whenever they write event-handling code, such as an ActionListener that gets called when a button is pressed, they need to handle things quickly. You never want to spend any more time than you have to processing on the event-handling thread, or your GUI will become nonresponsive and be unable to repaint itself efficiently. Taking on a larger task often means kicking off a separate "worker" thread from the event-dispatching thread and letting that run in the background. However, what many beginning Swing programmers forget is that Swing is not thread-safe, which means that if you have GUI updates from that worker thread, you should always execute them back on the event-dispatching thread. In short, this means you should create another Runnable thread that kicks off serially with other GUI updates on the event-dispatching thread. You can do so by executing it with a call to SwingUtilities.invokeLater(myGUIUpdateThread).
One note: I need to disclose that I used to work for O'Reilly Media, Inc. as an editor, back when it was called O'Reilly and Associates, Inc. That being said, I don't get any royalties from this book, nor am I rewarded for reviewing it in any way, shape, or form. All I get is a free copy of the book, which I can peruse whenever I need to dazzle people a little on my Swing programming.
All in all, Swing Hacks is a solid book that covers a great deal more than just the basic best practices that you would expect inside the standard encyclopedic Swing tome. It's a good book to have around when you need advice on working with components, you want to speed thing up a little, or you just want to make things a little jazzier.

Comments

Popular Posts