0N/A<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
0N/ACopyright (c) 1998, 2006, Oracle and/or its affiliates. All rights reserved. 0N/ADO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/AThis code is free software; you can redistribute it and/or modify it 2362N/Aunder the terms of the GNU General Public License version 2 only, as 0N/Apublished by the Free Software Foundation. Oracle designates this 2362N/Aparticular file as subject to the "Classpath" exception as provided 0N/Aby Oracle in the LICENSE file that accompanied this code. 0N/AThis code is distributed in the hope that it will be useful, but WITHOUT 0N/AANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/AFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/Aversion 2 for more details (a copy is included in the LICENSE file that 0N/Aaccompanied this code). 0N/AYou should have received a copy of the GNU General Public License version 0N/A2 along with this work; if not, write to the Free Software Foundation, 0N/AInc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2365N/APlease contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A <
META NAME="Author" Content="Eric Armstrong">
0N/A <
META HTTP-
EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
0N/A <
TITLE>swing package</
TITLE>
0N/A<
BODY bgcolor="white">
0N/A<
P>Provides a set of "lightweight"
0N/A(all-Java language) components that,
0N/Ato the maximum degree possible, work the same on all platforms.
0N/AFor a programmer's guide to using these components, see
0N/Atarget="_top">Creating
0N/Aa GUI with
JFC/
Swing</
a>, a trail in <
em>The Java Tutorial</
em>.
0N/AFor other resources, see
0N/A<
a href="#related">Related Documentation</
a>.
0N/A<
H2><
a name="threading">Swing's Threading Policy</
a></
h2>
0N/AIn general Swing is not thread safe. All Swing components and related
0N/Aclasses, unless otherwise documented, must be accessed on the event
0N/ATypical Swing applications do processing in response to an event
0N/Agenerated from a user gesture. For example, clicking on a {@code
0N/AJButton} notifies all {@code ActionListeners} added to the {@code
0N/AJButton}. As all events generated from a user gesture are
0N/Adispatched on the event dispatching thread, most developers are not
0N/Aimpacted by the restriction.
0N/AWhere the impact lies, however, is in constructing and showing a
0N/ASwing application. Calls to an application's {@code main} method,
0N/Aor methods in {@code Applet}, are not invoked on the event
0N/Adispatching thread. As such, care must be taken to transfer control
0N/Ato the event dispatching thread when constructing and showing an
0N/Aapplication or applet. The preferred way to transfer control and begin
0N/Aworking with Swing is to use {@code invokeLater}. The {@code
0N/AinvokeLater} method schedules a {@code Runnable} to be processed on
0N/Athe event dispatching thread. The following two examples work equally
0N/Awell for transferring control and starting up a Swing application:
0N/Apublic class MyApp implements Runnable {
0N/A // Invoked on the event dispatching thread.
0N/A // Construct and show GUI.
0N/A public static void main(String[] args) {
0N/A MyApp(String[] args) {
0N/A // Invoked on the event dispatching thread. Do any initialization
0N/A public void show() {
0N/A public static void main(final String[] args) {
0N/A // Schedule a job for the event-dispatching thread:
0N/A // creating and showing this application's GUI.
0N/A new MyApp(args).show();
0N/AThis restriction also applies to models attached to Swing components.
0N/AFor example, if a {@code TableModel} is attached to a {@code
0N/AJTable}, the {@code TableModel} should only be modified on the
0N/Aevent dispatching thread. If you modify the model on a separate
0N/Athread you run the risk of exceptions and possible display
0N/AAs all events are delivered on the event dispatching thread, care must
0N/Abe taken in event processing. In particular, a long running task, such
0N/Aas network io or computational intensive processing, executed on the
0N/Aevent dispatching thread blocks the event dispatching thread from
0N/Adispatching any other events. While the event dispatching thread is
0N/Ablocked the application is completely unresponsive to user
0N/Aprocessing when working with Swing.
0N/AMore information on this topic can be found in the
0N/Ain particular the section on
0N/A<
a name="related">Related Documentation</
a>
0N/A<
P>For overviews, tutorials, examples, guides, and other documentation, please see:
0N/A target="_top">The Swing Connection</
A>
0N/A target="_top">The Java Tutorial</
A>
0N/A target="_top">Online Training</
A> at the Java Developer Connection<
font size=-
2><
sup>SM</
sup></
font>
0N/A target="_top">Java Foundation Classes (JFC)</
A> home page