Tuesday, December 12, 2006

Foxtrot - Easy API for JFC/Swing - Documentation

Foxtrot - Easy API for JFC/Swing - Documentation: "The GUI freeze Problem

When you write Swing applications, you show a GUI to a user; the user clicks on some components (buttons, menus, etc.) to perform the desired action.
The code that executes the action is written in event listeners methods, and event listeners are always executed in the Event Dispatch Thread.
The Event Dispatch Thread is responsible for taking one event after another and processing it; the processing involves calling the event listeners's method, which are then executed. If an event listener requires a long time to be executed, then the Event Dispatch Thread cannot process the next event, which will then be waiting in the Event Queue.
If the pending event is a repaint event, the GUI cannot be repainted, so it appears to be frozen.
So resizing your window, overlapping it with another window, clicking on other components, all these events are queued but not processed until the time-consuming listener has finished.
The user feels the application has hung.
When the time-consuming listener finishes, all pending events are processed, and if they are quick to execute (like repainting events) it appears they're are executed like a storm.

Take a look at the following code.

Let's concentrate on the button's listener (the actionPerformed() method): the first statement changes the text of the button. This causes"