1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> 2 <HTML> 3 4 <HEAD> 5 <!-- 6 Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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. Oracle designates this 12 particular file as subject to the "Classpath" exception as provided 13 by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 26 or visit www.oracle.com if you need additional information or have any 27 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://docs.oracle.com/javase/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 import javax.swing.SwingUtilities; 72 73 public class MyApp implements Runnable { 74 public void run() { 75 // Invoked on the event dispatching thread. 76 // Construct and show GUI. 77 } 78 79 public static void main(String[] args) { 80 SwingUtilities.invokeLater(new MyApp()); 81 } 82 } 83 </pre> 84 Or: 85 <pre> 86 import javax.swing.SwingUtilities; 87 88 public class MyApp { 89 MyApp(String[] args) { 90 // Invoked on the event dispatching thread. 91 // Do any initialization here. 92 } 93 94 public void show() { 95 // Show the UI. 96 } 97 98 public static void main(final String[] args) { 99 // Schedule a job for the event-dispatching thread: 100 // creating and showing this application's GUI. 101 SwingUtilities.invokeLater(new Runnable() { 102 public void run() { 103 new MyApp(args).show(); 104 } 105 }); 106 } 107 } 108 </pre> 109 This restriction also applies to models attached to Swing components. 110 For example, if a {@code TableModel} is attached to a {@code 111 JTable}, the {@code TableModel} should only be modified on the 112 event dispatching thread. If you modify the model on a separate 113 thread you run the risk of exceptions and possible display 114 corruption. 115 <p> 116 As all events are delivered on the event dispatching thread, care must 117 be taken in event processing. In particular, a long running task, such 118 as network io or computational intensive processing, executed on the 119 event dispatching thread blocks the event dispatching thread from 120 dispatching any other events. While the event dispatching thread is 121 blocked the application is completely unresponsive to user 122 input. Refer to {@link javax.swing.SwingWorker} for the preferred way to do such 123 processing when working with Swing. 124 <p> 125 More information on this topic can be found in the 126 <a href="http://docs.oracle.com/javase/tutorial/uiswing/">Swing tutorial</a>, 127 in particular the section on 128 <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency in Swing</a>. 129 130 131 <H2> 132 <a name="related">Related Documentation</a> 133 </H2> 134 <P>For overviews, tutorials, examples, guides, and other documentation, please see: 135 136 <UL> 137 <LI><A HREF="http://www.oracle.com/technetwork/java/javase/tech/articles-jsp-139072.html" 138 target="_top">The Swing Connection</A> 139 <LI><A HREF="http://docs.oracle.com/javase/tutorial/" 140 target="_top">The Java Tutorial</A> 141 <LI><A HREF="http://www.oracle.com/technetwork/java/javase/training/index.html" 142 target="_top">Online Training</A> at the Java Developer Connection<font size=-2><sup>SM</sup></font> 143 <LI><A HREF="http://www.oracle.com/technetwork/java/javase/tech/index-jsp-142216.html" 144 target="_top">Java Foundation Classes (JFC)</A> home page 145 </UL> 146 147 @serial exclude 148 149 </BODY> 150 </HTML> 151