Even if you have never used the Thread class, you unwittingly are using Threads whenever you use Swing. To write even the simplest Swing program you must follow some simple thread rules to avoid trouble.
Thread Safety | Best Practice |
The EDT | Which Thread Am I On? |
invokeLater and invokeAndWait | The Sleep Error |
Thread Safe Swing Methods | Learning More |
There are two fundamental things to understand about Swing and threads:
The AWT equivalents are called java.awt.EventQueue.invokeLater( Runnable ) and EventQueue.invokeAndWait( Runnable ). The Swing versions are just wrappers around the AWT methods, so you might as well just use the AWT versions all the time. The Swing versions were stop gaps until the AWT versions were available.
The good news is, even though, with invokeLater and invokeAndWait, you are using the Runnable interface normally associated with starting new threads, you don’t actually have the overhead of creating a new Thread since you are just calling the run method on the already existing Swing thread.
Here is how you would wrap a piece of GUI-manipulating code to run from any thread.
// disable the abort JButton from a non-Swing thread. SwingUtilities.invokeLater( new Runnable() { public void run() { abort.setEnabled( false ); } } );
Most Swing methods are not safe including:
Container.invalidate
Container.revalidate
Container.validate
JButton.setText
JComponent.revalidate
JComponent.setBackground
JComponent.setEnabled
JComponent.setFont
JComponent.setForeground
JComponent.setVisible
JComponent.validate
JLabel.setText
JTextArea.setText
JTextComponent.setText
If you are unsure, check the Javadoc.
Originally you were allowed to construct new Swing objects on other threads and call
their methods, but once they were realised, — once
you had called pack/setVisible( true ), from that point on
you had manipulate the Components only from the Swing
thread (which is not the same thread as the main thread). Sun now recommends doing
all manipulation of Swing components, realised or not, via the Swing (aka
EDT) thread,
Here is the safer way to fire up a GUI with all the initialisation on the Swing thread:
The good news is, even though you are using Runnable, you don’t have the overhead of actually creating a new Thread since you are just calling the run method on the already existing Swing Thread.If you violate these rules, the entire system starts to behave unpredicably and irrationally. If by violating these rules, you manage to create two event-processing threads, life gets really interesting as they unpredictably fight with each other handling Swing events.
This page is posted |
http://mindprod.com/jgloss/swingthreads.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\swingthreads.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[18.220.13.15] |
| |
Feedback |
You are visitor number | |