- About the JFC and Swing
- Compiling and Running Swing Programs
- Questions and Exercises
- Example Programs
Compiling and Running Swing Programs
This section explains how to compile and run a Swing application. The compilation instructions work for all Swing programsapplets, as well as applications. The following are the steps you need to follow:
-
Install the latest release of the Java 2 platform, if you haven't already done so.
-
Create a program that uses Swing components.
-
Compile the program.
-
Run the program.
If you're new to writing and compiling programs that use the Java platform, read The Java Tutorial trail "Getting Started" first; it's available online and on the CD at: JavaTutorial/getStarted/index.html. That trail also explains common errors that users may encounter.
Install the Latest Release of the Java 2 Platform
You can download the latest release of the J2SE SDK for free from http://java.sun.com/j2se. Version 1.4.2 of the J2SE SDK is included on the CD that accompanies this book.
Create a Program That Uses Swing Components
You can use a simple program we provide, called HelloWorldSwing, that brings up the GUI shown in Figure 1. The program is in a single file, HelloWorldSwing.java.4 When you save this file, you must match the spelling and capitalization of its name exactly. If you prefer to type in the file's code yourself, you can find the full code starting on page 12 in Chapter 2. |
Figure 1. The HelloWorldSwing application.
Compile the Program
Your next step is to compile the program. Here's an example of compiling HelloWorldSwing.java:
javac HelloWorldSwing.java
If you can't compile, make sure you're using the compiler in a recent release of the Java platform, such as 1.4.2 included on this book's CD. Once you've updated your SDK, you should be able to use the programs in this book without changes. Another common mistake is installing the Java Runtime Environment (JRE) and not the full Software Development Kit (SDK) needed to compile these programs. Refer to the "Getting Started" trail of The Java Tutorial to help you solve any compiling problems you encounter.5 Another installation troubleshooting guide for the Java 2 platform is online at: http://servlet.java.sun.com/help/installation/.
Run the Program
After you compile the program successfully, you can run it. Assuming that your program uses a standard look and feelsuch as the Java, Windows, or GTK+ look and feelyou can use the interpreter to run the program without adding anything to your class path. For example:
java HelloWorldSwing
For programs that use a nonstandard look and feel or any other nonstandard code package, you must make sure that the necessary classes are in the class path. For example:
Solaris
java -classpath.:/home/me/lnfdir/newlnf.jar HelloWorldSwing
Microsoft Windows
java -classpath .;C:\java\lnfdir\newlnf.jar HelloWorldSwing
Alternatively, you can launch your program from a Web browser using Java Web Start.
Running Programs Using Java Web Start
Java Web Start is a technology that simplifies the distribution of applications. With a click on a Web page link, you can launch full-featured applications without a complicated download and installation process. The first time you launch an application, Java Web Start automatically downloads all necessary files. It then caches them on your computer so the application can be quickly relaunched from a desktop shortcut or from a Web browser. Java Web Start checks for updates each time you run a remote application and downloads any updated files automatically.
In 1.4.1 and later releases of the Java platform, Java Web Start is shipped as part of the platform. So, if you have J2SE or JRE v1.4.1 or higher, you already have Java Web Start installed. When you install a version of the Java platform with Java Web Start, on most platforms you'll see a Java Web Start shortcut on your desktop. (See Figure 2.)
Figure 2. The Java Web Start desktop shortcut.
To test whether your browser can launch an application using Java Web Start, point it to one of the following places and click the "Launch JavaTM Web Start" link:
-
JavaTutorial/tutorial/uiswing/14start/HelloJWS.html (on this book's CD)
-
http://java.sun.com/docs/books/tutorial/uiswing/14start/HelloJWS.html
You should see a Java Web Start splash screen. (See Figure 3.)
Figure 3. The Java Web Start splash screen.
The application is then downloaded, and you should see what's shown in Figure 4.
Figure 4. The HelloJWSYou can find the HelloJWS.java source files here: JavaTutorial/uiswing/14start/example-1dot4/index.html#HelloJWS. application launched from a Web browser using Java Web Start.
If you're unable to launch this application, you should check the Java Web Start Troubleshooting (page 733) guide in the Appendix.