tropo://techno/java/preloader
Java Preloader Secrets
[ Source | Javadoc ]

This is the Java Class Preloader that is used to "preload" java classes before you think they'll be used. It is typically used when the user is reading one web page and you think (hope) they'll follow a link to another page with an applet - well then on the first page you run the preloader and then if they spend enough time on the first page then by the time they get to the second, voilla, all the classes will already be loaded and the applet will "load" instantaneously. It works by calling Class.forName() on each class in a configurable list of classes -- this causes the class files to be loaded by the browser and then they're cached when they're later needed.

This is used on our Colour Explorer intro page to load classes just before they're used when the Colour Explorer really runs.

The source is in one file, TRPreloaderApplet.java and when compiled the bytecodes are only 1,700 bytes long. There is also a bit of javadoc on this too.

In a web page it's used as follows. Note that the width and height are set to 1 since we don't want this to be visible.

<applet code="tropo.applet.TRPreloaderApplet.class" width=1 height=1 codebase="/techno/java/lib">
	<param name=classes value="class1,class2,class2">
</applet>

This is how it's used on on the Colour Explorer intro page - classes are loaded in reverse order, though in retrospect that may not be necessary - but this is the most bombproof way to guarentee that they all load.

<applet code="tropo.applet.TRPreloaderApplet.class" width=1 height=1 codebase="/techno/java/lib">
    <param name=classes value="tropo.applet.TRAnotherLoaderApplet,tropo.awt.TRDraw,tropo.awt.TRColor,tropo.awt.TRBoxType,tropo.awt.TROrientation,tropo.awt.TRSeparator,tropo.awt.TRDelegatedCanvas,tropo.awt.TRTextFieldRO,tropo.awt.TRSlider,tropo.applet.slider.ColorCanvas,tropo.awt.TRPanel,tropo.awt.TRGridBagPanel,tropo.awt.TRBoxPanel,tropo.awt.TRBufferedCanvas,tropo.applet.slider.ColoredButton,tropo.awt.TRFlowPanel,tropo.awt.TRMouseListener,tropo.awt.TRCanvasPainter,tropo.applet.slider.ColorExplorerPanel">
</applet>

And just for completeness, the standard HTML trick of preloading images is done via putting something like this:

    <img src="big.gif" width=1 height=1>
on a page before "big.gif" is needed - then when it is later loaded it will appear instantly as it will be in the browser image cache. There doesn't appear to be any need to have the preloader applet load images as this image trick should do the necessary cache loading.

[ Source | Javadoc ]
[ tropo ]