What's Happening?
Home ] Up ] Illustrating The Problem ] [ What's Happening? ] Fixing the Problem ]

 

 

Did you notice that:

  • The "Click me!" button remains depressed, and doesn't "spring back" until after a long delay?
  • The background color similarly did not change to red until after the same delay?
  • The "Now try to click me!" button is not enabled, again, until after the same delay?
  • The whole GUI freezes for a long time?

What's Happening?

All of this is because the doWork() method was invoked from, and therefore runs in the context of, the Event Dispatch Thread.  All of the requests for changes to the GUI that the actionPerformed() method requested were entered in the queue for the event dispatch thread. However, since events are dispatched serially, one at a time, and because the doWork() method was hogging the event dispatch thread, none of the GUI requests were acted upon until the work had been completed, by which time the actionPerformed() method had undone its requests (actually, requested additional changes to the GUI which reversed the original requests.

The above program is a typical example of what happens when a listener for an event tries to do too much work in direct response to receiving the listener callback for its event.  Doing this kind of thing can result in GUIs which respond poorly to user interaction.  We've probably all experienced GUI programs which exhibit such poor behavior, but we can do better!

The page was last updated February 19, 2008