Monday, November 27, 2006

Waiting and Blocking Input

Those pesky users are always trying to submit forms more than once, or creating new files willy nilly while they're waiting for feedback.

This article addresses two strategies for avoiding Impatient User Syndrome. First, provide some feedback to show that the program is busy, and second, don't allow any additional input.

Now, Java provides a JProgressBar, which you can throw up in a Dialog, but those aren't always appropriate. Sure if you want to block all application input (or in the case of Java 1.6, all frame input), that'll work, and you can always disable one component at a time. But suppose, for instance, that you've got a single frame, with a couple of different sections or docked panels. If their operation is independent of one another, you certainly don't want to block them all with a dialog.

The following demo shows how to block input on individual components, groups of components, or an entire frame, as well as providing "busy" feedback. The class WaitIndicator installs a component in the JLayeredPane above the target component, which prevents any mouse input from reaching the target component, and displays an hourglass cursor over the target component. It also installs a key event handler to ensure that no key events reach the target component.

The WaitIndicator also provides a paint method, which you can use to provide whatever visual feedback is appropriate to your program. The default implementation fades to the target component's background color. The demo itself uses a SpinningDialWaitIndicator, which paints an Apple-like spinner (which you can also see in the animated icon demo applet a few entries back).





Source

4 comments:

Anonymous said...

Hi,

Its been a while you posted this, but I got to my question just now.

Trying to use SpinningDialWaitIndicator:

During the execution of a long task, I want to display a JOptionPane.showXXX

The dialog is displayed, but it does not paint correctly. I tried to show it in the EDT, outside, with no luck

Any ideas?
Thanks in advance

technomage said...

Are you trying to put the wait indicator on the JOptionPane dialog/contents or on a different window?

The wait indicator paints in the JLayeredPane of the target component, and will do so until disposed.

Anonymous said...

First off... your wait indicator is an impressive GUI enhancement. It seems to work well in the little bits of code I threw it in. Well done.

For the life of me, though, I can't figure out how to make it work with a JTree. I'm trying to throw the indicator in a custom TreeCellRenderer, putting a waiting status over the icon while data is loading ... but to no avail. Ideally, it should be on over the handler, but I can't figure that out either.

Any suggestions?
Cheers,
Kevin

Anonymous said...

I recently posted a question regarding how to incorporate SpinningDialWaitIndicators and JTrees.

I solved my problem using your SpinningDial animated icon. Works like a charm!

Cheers,
Kevin