The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Creating a GUI with JFC/Swing
Lesson: Getting Started with Swing

Running Swing Applets

This section describes how to run applets that use Swing components. For information on writing Swing applets, see How to Make Applets(in the Creating a GUI with JFC/Swing trail).

You can run Swing applets in any browser that has the appropriate version of Java Plug-in(outside of the tutorial) installed. Another option is to use a 1.2-compliant browser. Currently, the only 1.2-compliant browser available is the Applet Viewer utility provided with the Java 2 SDK.

To test whether your browser can run applets, go to HelloSwingApplet.html. You should see a box that looks like the following:

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

You can find the applet's source code in HelloSwingApplet.java(in a .java source file), and the HTML code for including the applet by viewing the HTML source for HelloSwingApplet.html. 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(outside of the tutorial) for details on downloading a free HTML converter.

Here is a more complex applet, which uses multiple class and image files.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.

The applet's source code is in AppletDemo.java(in a .java source file). It uses the files images/right.gif, images/middle.gif, and images/left.gif as well.

The rest of this section gives step-by-step instructions for running the preceding applets. For information on writing applets, see How to Make Applets(in the Creating a GUI with JFC/Swing trail).

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. If nothing else, you can always use Applet Viewer (appletviewer), which is distributed in the JDK(outside of the tutorial) . Java Plug-in supports certain versions of Netscape Navigator and Internet Explorer. See the Java Plug-in documentation(outside of the tutorial) 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 Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form