Indefinite Progress Indicator #47
Chapter 6, Transparent and Animated Windows
|
247
HACK
Figure 6-11 shows the slide-in window on Mac OS X. It’s less appropriate
on the Mac, and it will be obscured if the user has dock magnification
turned on, but it’s not really bad either.
Hacking the Hack
To expand this hack, the first thing you’d probably want to do is add some
kind of
MouseListener so that if the user clicks to acknowledge the appear-
ance of the slide-in window, you could react to it by removing the slide-in
window, bringing your application’s main window to the front, etc. Then
again, you can put live components in here, so there’s no reason you
couldn’t just generate a
JOptionPane, make a JDialog from it, grab the con-
tent pane of that
JDialog, and show it in the slide-in window. That would
give you real, active Swing buttons and handy
JOptionPane return values.
After all, that’s what the sheet example did.
H A C K
#47
Indefinite Progress Indicator Hack #47
Despite its numerous advanced widgets, Swing offers no efficient way to
show that a task of unknown length is in progress. This hack presents two
solutions to address this issue.
Have you ever watched an application do something, but not tell you what
that something is? Other applications let you know what’s going on but
don’t really tell you how long they will need to complete the task. For
instance, the Microsoft Windows copy dialog is famous for its silly (and
lengthy) nonprogress indicators. As a user, I find this very annoying; as a pro-
grammer, I know how difficult it can be to determine the duration of a task.
The Swing Solution
To address this issue, developers created particular widgets meant to show a
task of unknown length is in progress. You can see such a widget in
Mozilla’s installer. It displays an indefinite progress bar—also called a
Cylon—in which a small rectangle bounces back and forth between the two
horizontal edges. I have also seen indefinite progress bars filling like regular
progress bars, going backward once filled and starting all over again. The
Figure 6-11. Slide-in window on Mac OS X with dock on bottom of screen (left) and not
on bottom (right)
248
|
Chapter 6, Transparent and Animated Windows
#47 Indefinite Progress Indicator
HACK
idea of an indefinite progress bar is great, but most existing implementa-
tions are just wrong. Users know what a progress bar looks like, and they
also know how it is supposed to behave. It is a bad idea to present a familiar
widget acting in a very surprising way. Unfortunately, the Swing designers
followed this trend and added the
setIndeterminate(boolean)
method to
JProgressBar
. Check out Example 6-14.
This short example creates a new window containing an indefinite progress
bar. When you launch the program, you can see a small rectangle bouncing
back and forth within the progress bar’s bounds, as in Figure 6-12. The
result is much better on Mac OS X because it uses the native Look and Feel
automatically, as shown in Figure 6-13. Despite this visual improvement,
though, the result is still far from perfect.
There are several better ways to show that a task of unknown length is in
progress.
Example 6-14. An indefinite progress bar
import javax.swing.*;
public class CylonBar {
public static void main(String[] args) {
JFrame f = new JFrame("Progress");
JProgressBar p = new JProgressBar( );
p.setIndeterminate(true);
f.getContentPane( ).add(p);
f.pack( );
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
Figure 6-12. The indefinite progress bar offered by Swing
Figure 6-13. Swing’s indefinite progress bar with Mac OS X Look and Feel

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.