C H A P T E R T W E L V E
Not everything about Swing fits into nice little groupings of functionality.
Some of the cool stuff you can do involves cursors, event-dispatching, net-
working, and even the lights on the keyboard. So, here we present some
hacks that were just unique (or weird) enough to defy easy categorization.
H A C K
Display a Busy Cursor Hack #88
Use the setCursor( ) method and an animation thread to show a frame’s
One of Swing’s lesser-known features is the ability to change the mouse cur-
sor on a per-component basis. Because the cursor change happens very
quickly, you could combine this ability with some simple threading to cre-
ate an animated cursor. This hack will show you how to create an animated
cursor useful for showing a busy status.
Any Swing component—even a frame—can have a custom cursor. The
application as a whole will retain the normal mouse cursor, but when the
user moves over the appropriately configured component, the cursor will
change to whatever is set for that component. This behavior lends itself
nicely to restricting user access during long running processes because the
rest of the application can look and feel responsive while the portion that
represents the process is visually unusable by the custom cursor.
The best way to manage a short animation is by pre-generating your images
in an array. This lets you loop through the array rather than creating a
bunch of nasty conditionals. If you later expand the animation, you can just
add more images to the array, leaving the rest of the code untouched.