Previous | Next | Trail Map | Creating a User Interface | 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, then you don't need to 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.

Note: Before JDK 1.2 Beta 4, the Swing package names in JDK 1.2 were different from the Swing package names in JFC 1.1. Now, however, the package names are the same, so you can use exactly the same code for both releases.

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 Beta 4 release. You don't have to specify a class path; just compile as usual.

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 | Using the JFC/Swing Packages