Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Java Tip 87: Automate the hourglass cursor

Force Java's UI event queue to decide when to show the hourglass cursor

  • Print
  • Feedback
Judging the amount of time required to execute a given task in a development process is nearly impossible. Executing the task on the actual target machine, however, lets you actively monitor how long each task takes. Then you simply measure the execution time and change the cursor to an hourglass when necessary. Fortunately, most tasks for which you typically display the hourglass cursor are executed during the processing of user interface (UI) events. Placing code within the stream of UI events enables you to measure processing times and automate the hourglass as desired.

The solution

By extending the capabilities of Java's UI event dispatch queue, you can easily insert your own timing mechanism into the UI events stream. The event queue distributes every UI event that occurs in an application. By replacing the standard event queue with your own, you can intercept every event passing through the queue. Your queue can then measure the time it takes to process each event and change the cursor if needed.

In this article, I will provide the full source code to accomplish this tip. But first, let's examine the solution in more detail. You first create an event queue class that extends the standard java.awt.EventQueue class. The new event queue contains a special timer thread for measuring the processing time for each event. It starts the timer before dispatching an event. Then, if the event is not processed within your predetermined time limit, the timer thread changes the cursor to an hourglass. After processing is complete, the queue changes the cursor back to the default cursor if necessary.

Below you will find the implementation of the dispatchEvent() method for the new event queue.

14:     protected void dispatchEvent(AWTEvent event) {
15:         waitTimer.startTimer(event.getSource());
16:         try {
17:             super.dispatchEvent(event);
18:         }
19:         finally {
20:             waitTimer.stopTimer();
21:         }
22:     }


All UI events go through the EventQueue.dispatchEvent() method. This method delivers the events to the correct UI components, which then process the events. As you can see, the queue overrides the default behavior of dispatchEvent() and wraps the standard functionality of the EventQueue class in your timing routines.

Line 15 notifies your timing thread that an event is about to be dispatched so that it will begin measuring the event's execution time. Note that you are passing the event's source -- the object to which this event will eventually be delivered. If the timing thread determines that the event is taking too long to process, it will need to know the event's source to determine which java.awt.Window's cursor should be changed to the hourglass.

Line 20 stops the timer and, if necessary, changes the cursor back to the default cursor. A try-finally block wraps the event dispatch because the stopTimer() method must be executed regardless of whether an exception is thrown during event processing.

The startTimer() method is very straightforward. It simply stores a reference to the event's source and notifies the timer thread.

  • Print
  • Feedback

Resources