15.6. Swing Threads

Swing follows the familiar AWT event model and uses a single-threaded design for updating components. Recall from Chapter 11 (Handling Mouse and Keyboard Events) that painting of components and handling of listeners are both processed in a single event dispatch thread. As a consequence of the event model, two general rules apply for robust GUI design:

  1. If tasks in the event handling method require considerable CPU time, then execute these time-intensive tasks in a separate thread. Freeing the event dispatch thread to process other queued events yields a more responsive user interface.

  2. Make changes to the state of a realized (visible) Swing component only within the event dispatch thread and not within a user-defined thread. ...

Get Core Web Programming, Second Edition 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.