Java™ Applet Tips             

Although Java is supposed to be a cross-platform language, there are now so many implementations of it that it's hard to be sure what an applet will look like in a particular version of a particular browser without taking a look. This page gathers together a few tips, browser inconsistencies and irritations that you may want to consider when adding applets to your pages.

Including An HTML Alternative
A Java menu system might make your site un-navigable to users with old browsers that don't support Java. However, you can include as much standard HTML code as you like before the applet's closing </APPLET> tag. If you place HTML links here to all the pages on your menu, browsers that do support Java will ignore them; those that don't will ignore the <APPLET> and <PARAM> tags and just display that HTML.

Using The Dialog Font
Many applets place text centrally within an area. Buttons and menus are obvious examples, such as our TextButton. When the applet is run, a browser's own Java Virtual Machine determines the horizontal position of the text by measuring the width of the area available and the length of the text string. Some browsers (particularly Netscape Navigator 4.x) cannot accurately gauge the width of a string when it uses Java's Dialog font, imagining that it's longer than it actually is, and placing it left of center. You may wish to avoid using Dialog for centered text, and stick to Helvetica, Courier or TimesRoman.

Using Any Font At All
Many menu applets place lines of text in vertical columns. Examples are drop-down menus such as our ClassicMenu. In this type of applet, the vertical position of each line is determined as a factor of font-height, which allows Web authors to use a larger font without the lines of text beginning to overlap. All Netscape Navigator versions allow vastly more vertical space between each line of text than any other Java platform. Thus a menu containing 6 items in this browser will be longer than the same menu in Internet Explorer or Sun's own HotJava and Appletviewer. Because the same measurements are used to calculate the size of the menu needed to display these lines of text, Navigator will still create a long enough menu, but in some cases your applet height may not be sufficient for the lower entries to be visible. When setting up any applet that creates columns of text, always check the longest column in Netscape before publishing the page to ensure that applet-height is sufficient to display the longest columns. In Internet Explorer and other browsers this space at the bottom of the applet will be 'dead-space', but will at least be filled with your chosen background color (usually a parameter named BgColor in CodeBase applets).

Refresh/Reload
In some Netscape versions, the Reload command does not reread applet parameters, and does not repaint the applet correctly if you changed its dimensions before Reloading. Always use the 'power-reload': hold Shift while clicking the reload button. In Internet Explorer v4.0 and higher, the Refresh command does do its job properly, but a similar power-refresh (Ctrl+Refresh) will re-read the applet class file and all related files (such as images or text files used by the applet).

Security: Applets Calling External Files
'External files' can be thought of as anything but a Java .class file and the current HTML document. Many applets call external files such as sound-effects and images, and others (such as our own NewsAgent and Searchlight) read their parameters and other information from text-files. Clearly Java has to have some security measures to prevent a rogue applet on a Web site reading information from a visitor's own computer, but this type of security is largely unnecessary when the applet is reading files from its own host (as it is when you're setting up and testing pages on your home system).

Although Internet Explorer 3.02 suffered from security paranoia, Microsoft recognized how ludicrous and irritating this was and released a patch: IE4 and the patched 3.02 can read local files provided that they are in the same directory as the current HTML document or in a subdirectory. Netscape Navigator will sometimes (though, typically, not always) refuse to run an applet on your local system if it calls external files, displaying a SecurityException or NullPointerException message in its statusbar and Java Console window.

If an applet simply calls non-vital files such as sound effects, turn off the Sound option while testing in this browser. If the requirement to load external files is fundamental to the applet's use, you'll need to upload your pages to the Web server and test them in this browser via HTTP. Alternatively, you can use our free utility, ComTest, written specifically to get around this irritation. ComTest is available for download from CodeBase.


.