2
|
1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
2 |
<HTML>
|
|
3 |
|
|
4 |
<HEAD>
|
|
5 |
<!--
|
|
6 |
Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
7 |
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
8 |
|
|
9 |
This code is free software; you can redistribute it and/or modify it
|
|
10 |
under the terms of the GNU General Public License version 2 only, as
|
|
11 |
published by the Free Software Foundation. Sun designates this
|
|
12 |
particular file as subject to the "Classpath" exception as provided
|
|
13 |
by Sun in the LICENSE file that accompanied this code.
|
|
14 |
|
|
15 |
This code is distributed in the hope that it will be useful, but WITHOUT
|
|
16 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
17 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
18 |
version 2 for more details (a copy is included in the LICENSE file that
|
|
19 |
accompanied this code).
|
|
20 |
|
|
21 |
You should have received a copy of the GNU General Public License version
|
|
22 |
2 along with this work; if not, write to the Free Software Foundation,
|
|
23 |
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
24 |
|
|
25 |
Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
26 |
CA 95054 USA or visit www.sun.com if you need additional information or
|
|
27 |
have any questions.
|
|
28 |
-->
|
|
29 |
|
|
30 |
<META NAME="Author" Content="Eric Armstrong">
|
|
31 |
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
|
|
32 |
<TITLE>swing package</TITLE>
|
|
33 |
</HEAD>
|
|
34 |
|
|
35 |
<BODY bgcolor="white">
|
|
36 |
|
|
37 |
<P>Provides a set of "lightweight"
|
|
38 |
(all-Java language) components that,
|
|
39 |
to the maximum degree possible, work the same on all platforms.
|
|
40 |
For a programmer's guide to using these components, see
|
|
41 |
<a href="http://java.sun.com/docs/books/tutorial/uiswing/index.html"
|
|
42 |
target="_top">Creating
|
|
43 |
a GUI with JFC/Swing</a>, a trail in <em>The Java Tutorial</em>.
|
|
44 |
For other resources, see
|
|
45 |
<a href="#related">Related Documentation</a>.
|
|
46 |
|
|
47 |
<H2><a name="threading">Swing's Threading Policy</a></h2>
|
|
48 |
|
|
49 |
In general Swing is not thread safe. All Swing components and related
|
|
50 |
classes, unless otherwise documented, must be accessed on the event
|
|
51 |
dispatching thread.
|
|
52 |
<p>
|
|
53 |
Typical Swing applications do processing in response to an event
|
|
54 |
generated from a user gesture. For example, clicking on a {@code
|
|
55 |
JButton} notifies all {@code ActionListeners} added to the {@code
|
|
56 |
JButton}. As all events generated from a user gesture are
|
|
57 |
dispatched on the event dispatching thread, most developers are not
|
|
58 |
impacted by the restriction.
|
|
59 |
<p>
|
|
60 |
Where the impact lies, however, is in constructing and showing a
|
|
61 |
Swing application. Calls to an application's {@code main} method,
|
|
62 |
or methods in {@code Applet}, are not invoked on the event
|
|
63 |
dispatching thread. As such, care must be taken to transfer control
|
|
64 |
to the event dispatching thread when constructing and showing an
|
|
65 |
application or applet. The preferred way to transfer control and begin
|
|
66 |
working with Swing is to use {@code invokeLater}. The {@code
|
|
67 |
invokeLater} method schedules a {@code Runnable} to be processed on
|
|
68 |
the event dispatching thread. The following two examples work equally
|
|
69 |
well for transferring control and starting up a Swing application:
|
|
70 |
<pre>
|
|
71 |
public class MyApp implements Runnable {
|
|
72 |
public void run() {
|
|
73 |
// Invoked on the event dispatching thread.
|
|
74 |
// Construct and show GUI.
|
|
75 |
}
|
|
76 |
|
|
77 |
public static void main(String[] args) {
|
|
78 |
SwingUtilities.invokeLater(new MyApp(args));
|
|
79 |
}
|
|
80 |
}
|
|
81 |
</pre>
|
|
82 |
Or:
|
|
83 |
<pre>
|
|
84 |
public class MyApp {
|
|
85 |
MyApp(String[] args) {
|
|
86 |
// Invoked on the event dispatching thread. Do any initialization
|
|
87 |
// here.
|
|
88 |
}
|
|
89 |
|
|
90 |
public void show() {
|
|
91 |
// Show the UI.
|
|
92 |
}
|
|
93 |
|
|
94 |
public static void main(final String[] args) {
|
|
95 |
// Schedule a job for the event-dispatching thread:
|
|
96 |
// creating and showing this application's GUI.
|
|
97 |
SwingUtilities.invokeLater(new Runnable() {
|
|
98 |
public void run() {
|
|
99 |
new MyApp(args).show();
|
|
100 |
}
|
|
101 |
});
|
|
102 |
}
|
|
103 |
}
|
|
104 |
</pre>
|
|
105 |
This restriction also applies to models attached to Swing components.
|
|
106 |
For example, if a {@code TableModel} is attached to a {@code
|
|
107 |
JTable}, the {@code TableModel} should only be modified on the
|
|
108 |
event dispatching thread. If you modify the model on a separate
|
|
109 |
thread you run the risk of exceptions and possible display
|
|
110 |
corruption.
|
|
111 |
<p>
|
|
112 |
As all events are delivered on the event dispatching thread, care must
|
|
113 |
be taken in event processing. In particular, a long running task, such
|
|
114 |
as network io or computational intensive processing, executed on the
|
|
115 |
event dispatching thread blocks the event dispatching thread from
|
|
116 |
dispatching any other events. While the event dispatching thread is
|
|
117 |
blocked the application is completely unresponsive to user
|
|
118 |
input. Refer to {@link javax.swing.SwingWorker} for the preferred way to do such
|
|
119 |
processing when working with Swing.
|
|
120 |
<p>
|
|
121 |
More information on this topic can be found in the
|
|
122 |
<a href="http://java.sun.com/docs/books/tutorial/uiswing/">Swing tutorial</a>,
|
|
123 |
in particular the section on
|
|
124 |
<a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html">How to Use Threads</a>.
|
|
125 |
|
|
126 |
|
|
127 |
<H2>
|
|
128 |
<a name="related">Related Documentation</a>
|
|
129 |
</H2>
|
|
130 |
<P>For overviews, tutorials, examples, guides, and other documentation, please see:
|
|
131 |
|
|
132 |
<UL>
|
|
133 |
<LI><A HREF="http://java.sun.com/products/jfc/tsc/"
|
|
134 |
target="_top">The Swing Connection</A>
|
|
135 |
<LI><A HREF="http://java.sun.com/docs/books/tutorial/"
|
|
136 |
target="_top">The Java Tutorial</A>
|
|
137 |
<LI><A HREF="http://developer.java.sun.com/developer/onlineTraining/"
|
|
138 |
target="_top">Online Training</A> at the Java Developer Connection<font size=-2><sup>SM</sup></font>
|
|
139 |
<LI><A HREF="http://java.sun.com/products/jfc/"
|
|
140 |
target="_top">Java Foundation Classes (JFC)</A> home page
|
|
141 |
</UL>
|
|
142 |
|
|
143 |
@serial exclude
|
|
144 |
|
|
145 |
</BODY>
|
|
146 |
</HTML>
|
|
147 |
|