ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Topic: Best Practices, Java SE (Standard)

Multithreaded Swing Applications

Swing components are not inherently thread safe, and as a general rule, after Swing components have been made visible on the screen, you can only safely modify their data from the event thread. If you modify Swing component data from any thread other than the event dispatching thread, you must take precautions to ensure data integrity. An exception to this rule is the setText method on a JTextComponent or any of its subclasses, or any Swing component method whose documentation explicitly states it is thread safe. Sometimes, you cannot avoid modifying Swing component data from outside the event dispatching thread after it has been made visible. An example is spawning a thread to handle a long operation such as reading or writing a large file over the net. The advantage to spawning a new thread is that the user interface is made available to the user for other operations during the long file operation. The spawned thread is a new thread, and therefore, not on the event dispatching thread even if it is spawned by an event dispatching thread method. This article explains how to use the InvokeLater method and synchronize keyword to ensure data integrity when modifying data in Swing components. If you are new to multi-threaded programming, see Creating a Threaded Slide Show Applet for an introduction to the threaded applications.

Programming Language: Java

Updated: 09/18/2001
Organization: java.sun.com