Make
Your Browser Swing!
Implementing
Swing Applets on Netscape and IE
NOTE: This is a new version of an earlier article titled
"Swinging
Applets," which now resides in The Swing Connection Archive.
The main difference between the two articles is that the sample
program supplied with this article uses Swing's current set of
package names, while the applet supplied with the old article
used an older set of package names that were used prior to the
introduction of Swing 1.1 Beta 3 and JDKTM
1.2 RC1 (JavaTM 2).
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 you how to 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 one thing,
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?
Before
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.
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 your browser isn't Swing-ready, will advise you on what
to do about it.
IMPORTANT:
The sample applet used in this article was written and compiled
using Swing 1.1.1 beta 1. You can run it by using Swing 1.1 beta
3 or later, or by downloading and using JDK 1.2 RC1 or later.
But you can't use it with older versions of Swing or the JDK because
because they used a set of Swing package names that are now outdated.
If you'd like to examine an earlier version of the applet, which
will run under older versions of Swing and the JDK, see the article
titled "Swinging
Applets " that now resides in The Swing Connection Archive.
Configuring Your Browser for Swing
The
applet that you'll be implementing as you read this article is named
SwingingApplet.
There's a picture of it on the left. 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 article in this section titled "Plugging
into Swing." 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.5. 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.
IMPORTANT:
You don't have to set your system's CLASSPATH
variable if you want to access Swing applets using Java Plug-in.
Java Plug-in is not affected in any way by your system's CLASSPATH
variable. You need to set it only if you plan to run applets without
using Java Plug-in.
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:
- Navigate to the directory in which Swing is installed.
- Copy swingall.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 show up on your screen.
Setting
up Internet Explorer for Swing
It's also easy to
add Swing applet support to Internet Explorer 4.01's VM. To configure
IE's CLASSPATH
to run Swing applets, follow these steps:
On Windows NT:
- Double-click the System icon inside the Control Panel. When
the System Properties dialog box opens, place the following variables
in the lower list box, which is labeled "User Variables." (Be
careful not to change your system environment variables, which
appear in the upper list box.)
|
JAVA_HOME |
C:\JDK1.2.1 |
|
SWING_HOME |
C:\swing-1.1.1beta2 |
|
CLASSPATH |
.;%JAVA_HOME%\lib\classes.zip |
|
PATH |
%PATH%;%JAVA_HOME%\bin |
These settings assume that you have installed both the JDK and
the Swing tool set on drive C. If either these toolkits has been
installed on a different drive, substitute that drive's designator
wherever it is appropriate.
The settings also assume that you are using JDK 1.2.1 and Swing
1.1.1 beta 2. If you are using a different version of Swing, change
the setting in Line 2 as appropriate.
(It isn't always necessary to set the CLASSPATH
environment variable, although a setting is shown in the preceding
example. For example, you don't have to
set the CLASSPATH variable if you're using Java Plug-in and JDK
1.2. For more details, see the article titled "Plugging
into Swing.")
(NOTE: If you do use the CLASSPATH
variable, 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 box work, you can refresh your memory by reviewing
the README.txt
file that comes with Swing.)
- Click the Set button to apply your changes.
- Close the System Properties dialog box by clicking the Close
button.
- If your browser still fails to run Swing applets, shut down
and restart your system.
On Windows 95:
- Start your computer's MS-DOS text editor or your favorite text
editor.
- Open your computer's AUTOEXEC.BAT
file for editing.
-
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.)
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.
CAUTION:
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:
- From the Windows Start | Programs menu, open an MS-DOS style
console (command-prompt) window.
- Open regedit by executing the command
> regedit
Windows then opens its registry window, which looks like
this:
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:
- 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.
- Confirm that the status line at the bottom of the window shows
the entries that you have typed in, as shown in the illustration.
- In the right-hand pane of the regedit window, double-click the
Classpath icon.
Windows responds by opening an 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.
-
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-1.1.1beta2,
append this information to the text already shown in the
"Value data" text field.
C:\WINNT\java\classes;.;
C:\swing-1.1.1beta2\swing.jar;
C:\swing-1.1.1beta2\windows.jar;
C:\swing-1.1.1beta2\motif.jar;
- Click OK twice to exit from the Edit String dialog and the registry
editor.
- Reload this page, wait for SwingingApplet to load, and then
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 was Netscape 4.5, which comes ready to run JDK 1.2 and
Swing. When you use Java Plug-in (see "Plugging
into Swing"), 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
tasks related to the Java programming language that 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 way 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 when you specify both types in an APPLET tag definition,
it refuses to use both kinds of files at the same time. 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, there are only a couple of ways to solve these
problems: You can either move the mouse around just right
or minimize frames that haven't been repainted properly and
then repaint them. 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.
|