[an error occurred while processing this directive]
The Morgue Commentary PLAF Papers Friends Tech Topics Swing Text The Web Tech Topics What's Swing?
Index Archive Call 911 PLAF Papers Friends Tech Topics Swing Text Swing & the Web IDE Roundup Special Report Databank Page 2 Page One What's Swing?
JDK Docs Download JDK Swing API Docs Download Swing Java Tutorial
In this section

Swinging Applets
Implementing Swing Applets on Netscape and IE


IMPORTANT: The sample applet supplied with this article uses a set of Swing package names that are now outdated and work only with older versions of Swing. A new version of this article, "Make Your Applet Swing!", appears in the current "Swing and the Web" section in this issue. The sample applet presented with that article is an up-to-date applet that works under current versions of Swing.


By Mark Andrews and Nancy Schorr

Yes,  Virginia, you can write and run Swing applets in your Internet browser  right now, using the current version of Swing. And there are several ways  to do it.

Now that JavaTM Plug-in  software is available -- as reported in the "Plugging into Swing" article in this section --  you can use Sun's new Java Plug-in technology to write and distribute Swing applets that will run on any computer system using any browser. "Plugging into Swing" shows how you can use Java Plug-in to create and distribute your Swing applets.

But Java Plug-in, as powerful as it is, may not meet the needs of every  applet that you want to distribute. For example, Java Plug-in is designed  to be downloaded from a network, but some computers don't have network connections. And you might find it difficult to adapt Java Plug-in to certain combinations of browsers and operating systems that are sometimes found in multi-user corporate computer environments.

For situations such as these, several alternatives to using Java Plug-in  are available. This article shows how to implement some of them.


Is your browser ready for Swing? 

Navigator and IE logosBefore we start exploring the topic of Swing-aware browsers, let's perform  a little test that will tell you whether the browser you're using now has a native JVM (JavaTM virtual machine) that works with Swing. If it does, it can run Swing applets without using Java Plug-in.

To see whether your browser's VM is currently Swing-ready, click the "Test My VM" button, below. arrow_blue_down A window will then open, and you'll see a little test that will show you whether your browser is configured to run Swing applets -- and, if you're browser isn't Swing-ready, will advise you on what to do about it..

BtnVMTest 

NOTE: The sample application used in this article was written with Swing 1.0.3. To compile and run it using Swing 1.1, you'll have to convert its package names to the new Swing 1.1 format. You can use the PackageRenamer utility that's available for free downloading from Java Software.

 

 Configuring your browser for Swing

applet_captionThe applet that you'll be implementing as you read this article is named SwingingApplet. There's a picture of it on the right. To run the SwingingApplet program -- or any other applet that uses Swing components  -- you can use either the techniques described in this article or those described in the other article in this section, "Java Plug-in Power." If you choose to use the strategies described in the article you´re reading now, you'll need either a recent version of Microsoft Internet Explorer  (4.0 or later) or a recent version of the Netscape Navigator or Netscape  Communicator browser.

When this article was last updated, the newest  version of Netscape Navigator in general release was Version 4.06. For more details on the various versions of Netscape browsers and how they work with Swing, see the section headed "Versions of Netscape Browsers" farther down in this article.


Setting up Netscape for Swing

Once you´re sure that you´re using a correct version of Netscape, you can add Swing support  to your Netscape browser's VM in two different ways. One way is to place the  pathnames of the Swing classes in your system's CLASSPATH variable. You  can do that by following the same instructions that are listed under the  next blue subheading, "Setting up Internet  Explorer for Swing." Because that technique works for Netscape  browsers as well as for IE browsers, it might be the one you want to select  if you have both kinds of browsers installed on the same computer system.

Configuring Navigator without using CLASSPATH

There's also a way to configure the Navigator browser for Swing without using the  CLASSPATH variable. To that, follow these steps:

  1. Navigate to the directory in which Swing is installed.
     
  2. Copy swing.jar and all of Swing's other JAR files into Netscape's Java\Classes subdirectory. By default, the target directory for  this copying operation is C:\Program Files\Netscape\Communicator\ Program\Java\Classes

When you've completed those two steps, you're ready for action. Reload  this page, and SwingingApplet should now show up on your screen.


Setting up Internet  Explorer for Swing

It's also easy to add Swing applet support to Internet Explorer 4's VM. To configure IE's classpath to run Swing applets, follow these  steps:

On Windows NT:

  1. From the Windows control panel, double-click the System icon.
     
  2. When the System Properties dialog box opens, tab to the Environment  page.
     
  3. From the lower list box -- the one labeled "User Variables for  Administrator" -- choose the CLASSPATH variable.
     
  4. In the text field labeled "Value," add both the pathnames  and the filenames of your system's Swing JAR files to CLASSPATH variable shown in the "Variable" text field. When you type  in this entry, you can use any of the other environment variables that  are defined in the "User Variables" text area. For example,  if your Swing JAR files are installed in a directory that has been assigned  the environment variable %SWING_HOME%,  and if your JDK files reside in a directory that has been assigned the  variable %JAVA_HOME%,  you can set your system's user CLASSPATH variable to read:



    (NOTE:  Be sure to enter the filenames of your Swing JAR files, as well  as their pathnames, in the "Value" text field. Internet  Explorer does not recognize CLASSPATH entries that don't include filenames.)

    (ANOTHER NOTE:  If you don't remember exactly how all the environment variables shown  in the System Properties dialog work, you can refresh your memory by  reviewing the README.txt file that  comes with Swing.)
  5. Click the Set button to apply your changes.
     
  6. Close the System Properties dialog box by clicking the Close button.
     
  7. If your browser still fails to run Swing applets, shut down and restart  your system.

On Windows 95:

  1. Start your computer's MS-DOS text editor or your favorite text editor.
     
  2. Open your computer's AUTOEXEC.BAT file for editing.
     
  3. Add both the pathnames and the filenames of your system's Swing JAR  files to the CLASSPATH environment variable that you set up in your AUTOEXEC.BAT file when  you installed Swing. As you type in this entry, you can use any of the  other environment variables that appear above it in your AUTOEXEC.BAT  file. For example, if your Swing JAR files reside in a directory that  equates to an environment variable named %SWING_HOME%,  and if your JDK files reside in a directory that has been assigned the  variable %JAVA_HOME%,  you can add Swing support to your system by appending the following  command to your AUTOEXEC.BAT file:

    SET CLASSPATH=%CLASSPATH%;%SWING_HOME%\
       swing.jar;
    %SWING_HOME%\windows.jar;%SWING_HOME%\
       motif.jar;

    (NOTE: Be sure  to enter the filenames of your Swing JAR files, as well as their pathnames, in your SET  CLASSPATH command. Internet Explorer does not recognize CLASSPATH entries that don't include filenames.)

    (ANOTHER NOTE:  If you don't remember exactly how all the variables shown in your AUTOEXEC.BAT  file work, you can refresh your memory by reviewing the README.txt file that came with Swing.)
     
  4. If your browser still doesn't run Swing applets, shut down and restart  your system.


Configuring Internet Explorer  with regedit

The techniques outlined in the preceding subsections are not the only  strategies for adding Swing support to the IE browser. If you're familiar  with the Windows system registry and know how to use the Windows registry  editor (named regedit), you can add Swing support to Internet Explorer  by modifying a registry key named Java VM | Classpath.

 

    Warning imageCAUTION: If you decide to add Swing support to your IE browser using the regedit  method, remember that regedit can be dangerous; corrupting the Windows  registry can cause your system to fail. So before you start changing  registry keys using regedit, don't forget to save your current registry  settings by executing regedit's Registry | Export Registry Files menu  command.

 

Starting regedit

To start regedit, follow these two steps:

  1. From the Windows Start | Programs menu, open an MS-DOS style console  (command-prompt) window.
     
  2. Open regedit by executing the command

    > regedit
     
  3. Windows then opens its registry window, which looks like this:

Regedit screen shot 

Using regedit

As you can see, the registry editor window has two panes. The left-hand  pane contains a tree display that represents registry keys, and the right-hand  pane contains icons that represent key values. To modify the Java VM Classpath  entry in the system registry, these are the steps to follow:

  1. In the left-hand pane, expand the "HKEY_LOCAL_MACHINE" tree  node followed by "SOFTWARE," followed by "Microsoft."  Then choose "Java VM." Your regedit window now resembles the  one shown in the preceding picture.
     
  2. Confirm that the status line at the bottom of the window shows the  entries that you have typed in, as shown in the illustration.
     
  3. In the right-hand pane of the regedit window, double-click the "Classpath"  icon. Windows responds by opening the Edit String dialog box like the  one shown below. If a Java virtual machine is running on your system,  the "Value name" text field in the Edit String dialog already  contains some information about the Java classes used by the Windows  Java VM. The Java VM needs this information to run, so just leave it  there.

    "Edit String" dialog  
  4. Without disturbing the text already shown in the "Value data"  text field, type a semicolon after it and then add the pathnames and  filenames of your Swing JAR files. For example, if you have installed  Swing on your C drive in a directory named swing-0.7, append  this information to the text already shown in the "Value data"  text field.
     
  5. C:\WINNT\java\classes;.;c:\swing-0.7\swing.jar;
    C:\swing-0.7\windows.jar;c:\swing-0.7\motif.jar;

    Now the EditString dialog looks like this:

    "Edit String" dialog, filled in  
  6. Click OK twice to exit from the Edit String dialog and the registry  editor.
     
  7. Reload this page, wait for SwingingApplet to load, and click the "Your  browser swings!" button to test the applet.  

Versions of Netscape browsers 

When this article was last updated, the latest official release of Netscape at the time of this writing was Netscape 4.06, which comes JDK-1.1.5 ready (Netscape sometimes refers to JDK 1.1.5 as AWT 1.1.5). A preview release -- Netscape 4.5 -- was also available, but was not a final release and was not guaranteed to run reliably with Java Plug-in or with Swing. In fact, it was not supported for use with Swing at the article was last updated.

When you use Java Plug-in (see the Java Plug-in article), it doesn't really matter what Netscape JDK version you have because Java Plug-in always uses the version of JDK that the Plug-in is pointing to. However, in general, it's always best to use the latest versions of the browsers for other Java-related tasks the browser may need to perform.

You can tell what version of Java your browser is running by choosing the "Communicator|Java Console" menu item to bring up the Netscape Java Console window. Don't close the Netscape Java Console window after you've looked at it, because you won't be able to display it again unless you restart Netscape.

When you´re using Java Plug-in, you can tell which version of Java is running by consulting the Java Plug-in Console window. You can open that window from the Windows "Start|Programs" menu.

The best location to get the latest version of Netscape is to visit Netscape´s Download
site, which is at

http://home.netscape.com/download/index.html


Possible problems 

Even after you've upgraded your browser to use Swing, you still may encounter  a few problems that haven't been quite ironed out yet. For example:

When there are multiple archive files, Netscape Communicator loads only  the first one. So, when Netscape has unarchived the swing.jar file,  it may ignore the look-and-feel JAR files, blocking their use in applets.

Although Internet Explorer recognizes both class files and archive (JAR)  files, it refuses to use both kinds of files at the same time when you  specify both types in an APPLET tag definition. To work around this problem,  use the jar command to  convert any class commands that you use in your applets to JAR files.  Then you can list the names of both Swing's JAR files and your own JAR  files in the same APPLET tag definition.

Both Netscape Navigator and Internet Explorer have some mini-bugs that  can cause them to miss repainting operations in Swing applets. Currently,  about all you can do about these problems either is move the mouse around  just right or minimize and then restore frames that haven't been repainted  properly. We're working on more satisfactory workarounds.

Internet Explorer doesn't call the Class.getResource() method to get JAR files. As a result, you may encounter missing text and  graphics files in the SwingSet sample applet and in other applets that  use Swing.

[an error occurred while processing this directive]