Java Programming   Java Programming
 |Sofia Home | Content Gallery |
Home
Syllabus
Schedule
Lessons
Assignments
Resources

Lesson 14.1 Introduction to Java 2 and the JFC

This section will introduce you to the major features of the latest version of Java--the Java 2 platform. In addition, you'll learn how to download and configure the Java Foundation Classes for use in your Java 1.1 programs [if you use the built-in Microsoft JVM, for instance].

The Wrap Up exercises in this lesson do not require you to download the JFC, or to write and modify programs, even though I've provided instructions for doing so. 

What is Java 2?

The "Java 2 Platform" is the latest version of Java from Sun. Originally, this version of Java was called "JDK 1.2", but Sun felt that it was different enough to warrant a major revision in the version number. 

What's JDK? While the latest version of Java is now called, officially, the "Java 2 Platform", the Java Developer's Kit has kept the old numbering scheme. The current version of the "JDK" is now, officially, "The Java 2 Software Development Kit, Standard Edition, Version 1.3".

Java 2 differs from its ancestors in several major ways; here's a short overview of the major features. 

JFC Swing
Java 2 includes a new family of graphical user interface components called the Java Foundation Classes [JFC]. These classes are also known as the Swing classes. The JFC consists of 100% pure Java graphical user interface components that have vastly improved capabilities, compared to the Abstract Window Toolkit (AWT) components first provided with Java 1.

PLAF
Because the JFC components do not depend upon native peer classes, unlike the AWT classes, the appearance of a JFC JButton object will be the same whether it runs on a Mac, a Unix box, or under Windows. That doesn't mean that all JFC-based programs will look the same, however; JFC supports a feature called Pluggable Look & Feel that gives the users of your application the ability to change the look of your entire program without you having to do any extra work at all.

Accessibility API
JFC also provides a set of classes, called the Accessibility API, that provide a way to make your programs usable by people with disabilities. This API allows your JFC and AWT components to work with "assistive technologies"; these are used by people with and without disabilities and include screen readers, screen magnifiers, and speech recognition.

Java 2D API
The Java 2D API expands Java's graphics features to include fancy paint styles, defining complex shapes and controlling the rendering process. Using the Java 2D API you can finally write applications that make use of the AlienTongue font, provided it is installed on your user's machine. Significant support is also provided for image transformation, printing, and rendering.

Drag and Drop
While earlier versions of Java could use the system clipboard to transfer data, Java 2's Drag and Drop capabilities allow direct manipulation of objects between Java and non-Java programs. This feature will significantly improve application interoperability by enabling Drag and Drop between those applications incorporating Java technology and those not incorporating it.

Back to Top

Java Versions

The JFC classes are built into Java 2 but you can also download them separately for use with Java 1.1 and JDK 1.1-enabled browsers. 

Using the Java 1.1 version of the Swing classes allows you to run many of the examples using a Web browser equipped with the proper Java virtual machine. In fact, most of the examples from this lesson can be run using any browser that supports Java 1.1 [for example, Netscape Navigator or Microsoft Internet Explorer].

Only the JFC classes [including the PLAF and Accessibility] are available for Java 1.1. Other features, such as the Java 2D API and the Java 2 Drag and Drop support, are only available in Java 2. If you want to write Java applets that use these features, then your users will have to use the Java 2 Plugin, because no current browser [With the exception of Navigator 6 and Mozilla] supports Java 2. [You'll learn how to use the Java 2 Plugin and the HTML converter in Lesson 14.6].

Downloading and Installing Swing

If you have downloaded and installed Java 2, you already have the Swing classes installed. For JDK 1.1, however, you have to go through a little extra work. Here are the steps you should follow:

Step 1: Download Swing 

A. You can download the Swing classes from the Sun Web site. The download file is about 13 MB, so it will take a while if you have a slow connection. The Swing download site is at: http://java.sun.com/products/jfc/download.html

IMAGE:The Swing 1.1.1 download page [1/2 size. Click to show full size]

Once you arrive at the site, shown here, you'll be given an opportunity to download Swing in two different packages: an auto-installer version that works on Solaris, Win32, and the Mac Os, and a zip-tar file version that must be manually installed.

B. To use the auto-installer version, use the drop-down list to specify your operating system, as shown here, and then click on the continue button. Note that you must already have a 1.1 Java Virtual Machine [or greater] installed. [Click on image to enlarge].

IMAGE:Selecting a Swing download file format [1/2 size. Click to show full size]

C. You must read and agree to the license terms before you can continue downloading Swing. If you don't agree, the download will be cancelled. [The license doesn't cost you anything, even if you want to distribute the Java programs you've written using Swing.]

IMAGE:The Swing 1.1.1 download license page [1/2 size. Click to show full size]

D. Once you've read and "signed" the license agreement, you're ready to start the download. The download page, shown here, shows you the name of the file and its size; you may want to write these down so you'll be sure that the entire file was downloaded. [If your network connection breaks while downloading, it's possible that you might get only part of the file.]

IMAGE:The Swing 1.1.1 download page [1/2 size. Click to show full size]

Click on FTP download for fastest download speed. If you are behind a firewall, however, you may have to use HTTP instead.

E. Before you can download the file, you'll need to decide where to store it. This is merely a resting place for Swing, not its final destination. Select a directory in which to place the downloaded file, and make a careful note of its name, and where you've placed it; you'll have to retrieve it later to actually install the software. [Both the TEMP directory, or a dedicated DOWNLOAD directory are good choices.]

IMAGE:Selecting a directory to store the downloaded file, using Windows Explorer [1/2 size. Click to show full size]

F. Once you've selected a location for the downloaded file, you'll be given a progress bar that lets you know how long the download will take. As you can see in the picture [click to enlarge], the downloaded file is about 12 MB. On a fast [DSL or Cable Modem] link, this should take about 5 minutes; on a slow link, you could spend a few hours.

IMAGE:Progress dialog when downloading Swing 1.1.1 [1/2 size. Click to show full size]

Back to Top
Step 2: Install Swing 

A. Once the Swing installer file has been downloaded to your computer, it's time to install Swing. Begin by using Windows Explorer [or the Mac Finder] to find the downloaded file--you do remember where you saved it, don't you???--and double click the file entry. In the image shown here, the file, named Swing1_1_1-win.exe, is located in the C:\downloads\Java-Dev-Tools directory.

swing install

B. When you start the installer, as shown in this image, it first must decompress the file you downloaded. Although the file was about 12 MB--which is not really small--it actually contains about 50 MB of software.

IMAGE:The Swing 1.1.1 installer progress dialog [1/2 size. Click to show full size]

C. The installer program walks you through most of the steps necessary to install Swing on your machine. To do this, it uses a "Wizard-like" interface that asks you a series of questions. At each point, you're given the option of cancelling the installation, of returning to the previous step [when possible], or of going forward by pressing the Next button.

IMAGE:The Swing 1.1.1 installer initial welcome screen [1/2 size. Click to show full size]

The picture shows the first of the "Wizard" pages.

D. Even though you've already signed one license agreement when you downloaded the software, the installer makes you sign another before you can continue. This really isn't as paranoid as it seems, because it is possible to obtain the Swing classes on CD-ROM from Sun or as part of another product. This makes sure that everyone who installs the software has read--and agreed to--the licensing agreement.

IMAGE:The Swing 1.1.1 installer license acceptance screen [1/2 size. Click to show full size]

E. Once you've signed the license agreement, the installer displays the Swing "ReadMe" file. You will need this information later, but right now it really doesn't do you any good. Since the installer doesn't give you any way to print out the instructions, your best bet is just to click on Next, and go on. You can print the "ReadMe" instructions later.

IMAGE:The Swing 1.1.1 installer Read-Me text screen [1/2 size. Click to show full size]

F. The next step is to choose an installation folder. Under Windows, the default folder--C:\Program Files\Swing-1.1.1, is not ideal, because it contains embedded spaces. To change that, simply add your own folder name as shown in this illustration.

IMAGE:The Swing 1.1.1 installer directory selection dialog [1/2 size. Click to show full size]

G. The install program will automatically create icons for running the sample programs if you wish. If you don't want to install the sample programs, then you should check the "Don't create shortcut icons" radio button.

IMAGE:The Swing 1.1.1 installer icon selection dialog [1/2 size. Click to show full size]

H. The actual installation program is a Java application program which requires a Java virtual machine. This is not necessarily the same JVM you intend to use with Swing; it should be JDK 1.1.7 or later, however. If the JVM you wish to use is not automatically detected by the installer program, you can click the "Choose Another..." button to browse your hard-disk to find the one you want.

Choose JVM

I. At this point, you're given an opportunity to install the features you want. Since you're a programmer, you'll want to have the developer documentation. Click the Customize button, shown in the illustration, to select which developer options you want to install.


IMAGE:The Swing 1.1.1 installer install-set customizer screen [1/2 size. Click to show full size]

J. When you select customize, you can choose which of the options you actually want to install. You'll need the Docs to develop Swing programs, as well as the libraries. The Demos and the Source code are optional. When you've made your selection, press Install, and the actual installation will begin.

IMAGE:The Swing 1.1.1 installer install-set selection dialog [1/2 size. Click to show full size]

K. At this point, the installer program will start copying the files you specified to the directory you specified. The progress dialog, shown here, will keep you informed of the progress. Once the installation is finished, you're ready for the next step: configuration.

IMAGE:The Swing 1.1.1 installer final progress screen [1/2 size. Click to show full size]

Back to Top
Step 3: Configure Swing

Before you can actually use the Swing classes, you'll need to configure your development environment, and, optionally, your Web browser.

JDK 1.1 or Java 2?

These configuration instructions are only necessary if you are using Swing with JDK 1.1; if you are using the Java 2 Software Development Kit, you don't have to download the Swing classes, or do any configuration at all. 

To run your applets under Java 2, however, you'll have to use the Java Plugin, and convert your <APPLET> tags for early versions of Netscape. Lesson 14.6 shows you how to do that.

Here are the steps to follow to configure the Java 1.1 JDK and your Web browser to use the JFC classes:

A. Swing and the JDK

To use the JDK command-line tools--javac.exe, appletviewer.exe, and so on--you must tell the operating system where to find the executable files. If you use Windows 95 or 98, there are two ways you can do that.

  1. You can modify the AUTOEXEC.BAT file that runs when your program starts up to modify your PATH and CLASSPATH environmental variables. The Swing Readme.txt file has instructions about how to modify your AUTOEXEC.BAT file if you decide to go this route. The problem with this is that it makes it difficult to use multiple JDK versions.
  2. In Lesson 2.5, Using the JDK, you learned how to create a DOS Shortcut to set up the environment each time you want to use the JDK. This technique makes it easy to have several different versions of the JDK on your system, and to quickly switch between them.

Windows NT - If you are using Windows NT, adjust these instructions as shown in Lesson 2.5.

B. Modifying Your Shortcut

To modify your JDK Shortcut to use the Swing classes, you simply modify the StartJDK118.bat file used in Lesson 2 by adding one additional line:

SET CLASSPATH=.;C:\jdk\lib\classes.zip;C:\swing\swingall.jar

This assumes that you originally installed the JDK 1.1.8 in the directory C:\jdk, and that you installed the Swing classes in C:\swing. If you installed into a different directory, you'll need to adjust the line as necessary.

The completed batch file should look like this:

IMAGE:The StartJDK118.bat batch file inside notepad with the CLASSPATH adjusted to use the Swing classes

C. Testing Your Configuration

Once you've modified your JDK Shortcut, test it out by downloading and compiling the file JLabel01.java it with javac:

C:\JavaProjects> javac JLabel01.java

If you don't see any error messages, then your JDK is correctly configured to develop and run Swing applications and applets. You can try out the JLabel01 applet by 

  • Creating the HTML file JLabel01.html using the code shown here:
    <applet code=JLabel01 width00 height00></applet>

    • Running the applet using appleviewer

Here's what the program looks like when it runs.

IMAGE:The JLabel01 applet running inside appletviewer

If you see an error message that says "Class not found" then you need to double-check your CLASSPATH setting. If you get an error message that says "Bad path or file name" then you need to correct your PATH setting.

D. Configuring Your Browser

If you want to run a Swing applet inside a Java 1.1-enabled Web browser [IE4 or Navigator 4.06 and later], you need to modify your system so that the browser can find the new classes. 

Here are four different ways to do that. These instructions are taken, and modified, from the Readme.txt file supplied with the SwingSet sample program included with the JFC.

1. Modify the System CLASSPATH

This should work with both IE and Navigator. Use your favorite ASCII editor to add the CLASSPATH to the C:\AUTOEXEC.BAT file. Use the same CLASSPATH you used to configure the JDK. After making the modification, you need to restart the computer.

2. Copy swingall.jar to the Navigator Classes directory

This works only with Navigator 4.06 and later. Copy the swingall.jar file from your Swing home directory to the Navigator Program\Java\Classes directory, as shown here.

IMAGE:Locating the Navigator classes directory using Windows Explorer

When you run your first Swing applet, you should expect to wait about a minute for the program to load. Subsequent Swing applets will start up much faster.

Here is an example of the JLabel01 applet running in Netscape Navigator:

IMAGE:Running the JLabel01 applet in Netscape Navigator

3. Copy and expand swingall.jar into the IE classes directory

This works in IE4. [I haven't checked in IE5.] You just expand all of the Swing classes into IE's CLASSES directory.

a. Start the JDK so you are setting at a DOS command prompt
b. Copy swingall.jar to C:\WINDOWS\JAVA\CLASSES
c. Change directories to C:\WINDOWS\JAVA\CLASSES
d. Type jar xf swingall.jar [This takes a minute or two]
e. Delete swingall.jar

Once you are finished, you should be able to run Swing applets in IE. Because the classes are now separate, IE doesn't have to load the entire library [like Navigator apparently does]. Thus, starting up a Swing applet in IE is much faster than in Navigator. Here's what the JLabel01 applet looks like when it runs inside IE4:

IMAGE:Running the JLabel01 applet inside IE4

4. Load the Swing classes over the network

If you put the Swing class libraries in the same directory as your applet's class files, then any JDK 1.1 enabled browser can, theoretically, download the classes over the net. 

Unfortunately, this is usually not a viable option, because it can take several minutes for your applet to start running, since the Swing class libraries have to be completely downloaded before the applet can run. I have also been singularly unsuccessful in getting this to work with the current version of Netscape. [It did seem to work in earlier versions.] If you want to see how long it takes, then you can look at the following applet using IE. [If you try it with Netscape, let me know about your experience. I was not able to get this to work.]:

http://csjava.occ.cccd.edu/~gilberts/swing/V4Applet.html

Something to Talk About

In this lesson, you learned about some of the new Java 2 features. You also learned how to download, install, and configure the Java Foundation Classes for use with JDK 1.1.

Here are some questions to think about:

  1. Can you think of one advantage and one disadvantage of using the Java Foundation Classes, instead of the AWT?
  2. When would you choose to use the JFC along with Java 1.1, rather than using Java 2, which has the JFC classes built in?
  3. After reading through the different methods of using JFC with the current crop of Web browsers, which would you select to use if you wanted to distribute an applet that used JFC?

Please continue to the next section of this lesson.

 

Back to Top

 

Content Developed by Stephen Gilbert, Licensed under a Creative Commons License
Published by the Sofia Open Content Initiative
© 2004 Foothill-De Anza Community College District &The William and Flora Hewlett Foundation