Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using the JFC/Swing Packages

Running a Swing Applet

You can run Swing applets in any browser that has Java Plug-in installed. Other options are to use a JDK 1.1-compliant browser that can find the Swing classes, or to use a 1.2-compliant browser. Currently, the only 1.2-compliant browser available is the Applet Viewer utility provided with JDK 1.2. For information on setting up 1.1-compliant browsers to work with Swing, see Make Your Browser Swing, an article in The Swing Connection.

The only required difference between a non-Swing applet and a Swing applet is that a Swing applet must inherit from the JApplet(in the API reference documentation) class, rather than directly from the Applet class. For general information about applets, see Writing Applets(in the Writing Applets trail)

Do you see a box just below this paragraph that contains the text "You are successfully running a Swing applet"? If so, then your browser is configured correctly.


Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to view HelloSwingApplet.html, specifying swing.jar in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.

You can find the applet's source code in HelloSwingApplet.java, and the HTML code for including the applet by viewing the HTML source for this page. The bad news is that the HTML code for including the applet is rather convoluted. The good news is that you can generate the HTML code automatically from a simple <APPLET> tag. See the Java Plug-in documentation for details on downloading a free HTML converter.

Here is a more complex applet, with multiple class files and an image file. The applet's source code is in AppletDemo.java. It uses the file images/middle.gif, as well.

The rest of this page gives step-by-step instructions for running the preceding applets.

Step by Step: Running a Swing-Based Applet

  1. Find a 1.1 or 1.2 browser or download Java Plug-in into a supported browser. Make sure you have the latest version of the browser and plug-in, since later versions tend to have bug fixes that make Swing work better. Two 1.1 Java browsers are the HotJava browser and the Applet Viewer (appletviewer), which is distributed in the JDK. A 1.2 version of the Applet Viewer is distributed in JDK 1.2. Java Plug-in supports certain versions of Netscape Navigator and Internet Explorer; see the Java Plug-in documentation for details.

  2. If you're using a 1.1 browser without Java Plug-in, determine how to load the Swing JAR file into your browser. See Setting the Browser's Class Path for an example of putting the Swing JAR file into the Applet Viewer class path. See Make Your Browser Swing in The Swing Connection for examples of doing the same for Internet Navigator and Netscape Navigator.

  3. Point the browser at this page. If you can see the first applet but not the second (or the second without any images), please send mail to tutorial@java.sun.com describing what you see in the applet area and in the Java Console.


Previous | Next | Trail Map | Creating a User Interface (with Swing) | Using the JFC/Swing Packages