Running Applets


You want to run an applet.


Write some HTML and point a browser at it.


An applet is simply a Java class that extends java.applet.Applet, and in doing so inherits the functionality it needs to be viewable inside a web page in a Java-enabled web browser.[8] All that’s necessary is an HTML page referring to the applet. This HTML page requires a minimum of three attributes , or modifiers: the applet itself, and the width and height it needs on-screen, in screen dots or pixels. This is not the place for me to teach you the syntax of HTML -- there is some of that in Section 17.2 -- but I’ll show my HTML applet template file. Many of the IDEs will write a page like this for you if you use their “build new applet” wizards.

<HEAD><TITLE>A Demonstration</TITLE></HEAD>
<H1>My TEMPLATE Applet</H1>
<APPLET CODE="CCC.class"  WIDTH="200" HEIGHT="200">

You can probably intuit from this just about all you need to get started. For a little more detail, see Section 17.2. Once you’ve created this file (replacing the CCC with the actual name of your applet) and placed it in the same directory as the class file, you need only tell the browser to view the HTML page, and the applet should be included in it.

All right, so the applet appeared and it even almost worked. Make a change to the Java source and recompile. Click the browser’s Reload button. Chances are you’re still running the old version! Browsers aren’t very good at debugging applets. You can sometimes get around this by holding down the Shift key while you click Reload. But to let you be sure, there is a program in the JDK known as Appletviewer , a kind of mini-browser. You need to give it the HTML file, just like a regular browser. Sun’s AppletViewer (shown in Figure 1-12 under MS-Windows) has an explicit reload button that actually reloads the applet. And it has other features such as debugging hooks and other information displays. It also has a View->Tag menu that lets you resize the window until the applet looks best, and then you can copy and paste the tag -- including the adjusted WIDTH and HEIGHT tags -- into a longer HTML document.

Sun JDK AppletViewer

Figure 1-12. Sun JDK AppletViewer

The MacOS X runtime includes Apple’s own implementation (shown in Figure 1-13), which is more colorful but slightly less featureful -- I could not find the Reload item in its menu. It does, however, let you load a new HTML file by typing (or browsing), so you can get the same effect as Reload just by clicking on the Open button again.

Apple MacOS X applet launcher

Figure 1-13. Apple MacOS X applet launcher

Neither the Sun version nor the Apple version is a full applet runtime; features such as jumping to a new document do not work. But it is a good tool for debugging applets. Learn to use the AppletViewer that comes with your JDK or IDE.

See Also

The bad news about applets is that they either can’t use features of newer Java versions or they run into the dreaded browser-incompatibility issue. In Section 23.6, I show using the Java Plug-in to get around this. In Section 23.12, I talk about Java Web Start, a relatively new technique for distributing applications over the Web in a way similar to how applets are downloaded.

[8] Includes Netscape, MS Explorer, Sun’s HotJava demonstration browser, and others.

Get Java Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.