Course Links

Exercises

Resources

External

Overview

Designed properly, many programs that feature graphical interaction can be run in either a stand-alone or an applet environment with very little change. Basically, the classes that embody the graphical interaction are created within a wrapper responsible for setting up the desired environment. Different wrappers result in different environments. It is even possible to create a class that can be used in either an applet or stand-alone manner, depending upon how it is invoked. (Of course, some differences may be unavoidable. Applets cannot usually access system resources for security reasons.)

Two Simple Wrappers

Although there are other ways to structure a graphical program, this tutorial will assume the use of Java Swing elements. The basic building block of such programs is one or more JComponent objects, placed inside a Panel. The Panel may have been created by the program as part of a JFrame, or (for applets) it may be some portion of the browser window.

Here is a simple example, using the class GUIdemo, which is a subclass of JComponent. For this example, a separate wrapper class is used to display the GUI in each of the two environments. The stand-alone program may be run from the command line as usual:

java GUIdemoApplication

An applet, in order to be run, must be included in a web page, as provided in the HTML file. After clicking on the link, choose View Source in the browser to see the raw HTML. The portion that includes the applet looks like this:

<applet code="GUIdemoApplet.class" height="40" width ="120">Applet</applet>

Note that the code must reference the .class file produced by javac, not the .java source.

Looking at the Code

The GUI manager GUIdemo is designed like most of the other GUI managers we have seen. It includes two methods that handle the GUI setup: createAndShowGUI() and createComponents(). These are used exactly as we have seen them before.

The wrapper code is boilerplate that can remain unchanged, except for the name of the GUI manager class to be created. Note that the entry points to the two versions are slightly different: the application calls createAndShowGUI(), which creates a window (i.e., a JFrame) and then calls createComponents() to fill in its content pane. The applet, on the other hand, will use the window area provided by its surrounding web page, so it can call createComponents() directly. (If we had instead called createAndShowGUI() as in the application, nothing would have broken, but the GUI would have popped up in a separate window instead of being embedded in the web page.)

Combining the Classes

Since the two wrapper classes don't do very much, it is tempting to try to combine them somehow with the GUI manager itself. This can in fact be done; the class GUIdemoCombo.java contains both a main() and an init() method, so it can function as either an application or an applet. (Of course, to view it as an applet it must be included in a web page.)

One final comment: if you don't have easy access to a web browser, java includes a command that can parse a web page and run all the applets in them as though they were embedded in a page. Try this, after downloading the files above:

appletviewer GUIdemoApplet.html
appletviewer GUIdemoCombo.html