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

Getting Started with Swing

This page tells you how to compile and run a Swing application. If you wish, you can skip directly to A Tour Through "Hello Swing", which guides you through the code for a simple application. If you're interested in using Swing in applets, you'll also want to read Running a Swing Applet.

The following instructions assume that you've written and compiled Java programs before. If you haven't, please follow the instructions in The "Hello World" Application(in the Getting Started trail). Once you've successfully compiled and run the "Hello World" application, you should be able to set up and start using your Swing programming environment. Just follow these steps:

  1. Download the latest JDK release, if you haven't already done so.
  2. Download the latest JFC release, if you need to.
  3. Create a program that uses Swing components.
  4. Compile the program.
  5. Run the program.

Download the Latest JDK Release

The Swing software works with both JDK 1.1 and JDK 1.2. As a rule, you should download the latest version of either JDK 1.1 or JDK 1.2.

Download the Latest JFC Release

If you're using the 1.1 JDK release, you need to download the latest version of JFC 1.1.

If you're using JDK 1.2, don't download the JFC -- it's already included in the JDK 1.2 release.

Create a Program that Uses Swing Components

You can use a simple program we provide, called HelloSwing. It's explained in A Tour Through "Hello Swing". Please download the file HelloSwing.java and save it in a file. The spelling and capitalization of the file's name must be exactly like this: HelloSwing.java.

Compile a Program that Uses Swing Components

Your next step is to compile the program.

If you're compiling with JFC 1.1, you need to add the Swing class archive, swing.jar, to the class path. You can find the swing.jar file in the top directory of the JFC release.

If you're compiling under JDK 1.2, then you don't need to worry about the Swing class archive -- it's already included in the 1.2 JDK release. You don't have to specify a class path; just compile as usual.


Note: If you can't compile HelloSwing.java, it's probably due either to having the wrong files in your class path (if you're using JDK 1.1) or to a package name mismatch. Currently, Swing 1.1 Beta 3 is the only release that uses the package names used in HelloSwing.java. Here is how to change HelloSwing.java to use the old package names:
//import javax.swing.*;       //comment out this line
import com.sun.java.swing.*;  //uncomment this line
See Swing Package Names for more information about differences in package names.
Here's a general explanation of how to compile a Swing application with JFC 1.1.
  1. Make a note of where your copy of the JFC (Swing) release is installed. You'll need this to be able to find the Swing class archive, swing.jar. You might want to set the environment variable SWING_HOME to the top directory of the Swing release.


    Note: Don't bother unarchiving swing.jar!

  2. Make a note of where your copy of the JDK release is installed. You'll need this to be able to find the proper versions of the JDK classes and interpreter. You might want to set the environment variable JAVA_HOME to the top directory of the JDK release.

    The JDK classes are in the lib directory of the JDK release, in a file called classes.zip. Don't uncompress that file! The Java interpreter is in the bin directory of the JDK release.

  3. Compile the application, specifying a classpath that includes the swing.jar file, the JDK classes.zip file, and the directory containing the program's classes (usually "."). Be sure that the JDK classes.zip file and the interpreter you use are from exactly the same release of the JDK!

Run the Program

Once the program has successfully compiled, you can run it.

If you're using JFC 1.1, you need to make sure that the class path includes not only what you needed to compile the file, but also the archive file for the look and feel the program uses. The JavaTM Look and Feel, which is the default, is in the swing.jar file. The Windows look and feel is in windows.jar, and the CDE/Motif look and feel is in motif.jar. The "Hello Swing" application uses the Java Look and Feel, so you need only swing.jar in the class path.

If you're using JDK 1.2, then you don't have to specify a class path, as long as you use one of the following look and feels: Windows, CDE/Motif, or Java Look and Feel.

What Next?

If you've gotten this far, you're ready to start writing some Swing code! Here's where you might want to go from here:


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