Convert a Java app into an applet (June 24, 1999)

The JavaWorld experts answer your most pressing Java questions -- every week

Q: How do I convert a Java application to an applet?

A: Transforming a Java application to an applet can be either simple or difficult, depending on the requirements. At a minimum, every applet requires that at least one HTML page refer to it with suitable parameters; a Java-enabled browser to display that HTML page is needed as well. If you are using the applet only on your own machine, you can refer to this HTML page simply with a URL of the form file://C:\your\directory\your.html (on Windows) or file:///your/directory/your.html (on Unix). The HTML page must at least contain the following:

. Your applet will be able to load all the classes it needs from the codebase, which may be specified with a URL as above (but ending in a directory name or jar file name).

Let's look at this in more detail:

  1. Imagine a pocket calculator. No matter how fancy, it is essentially self-contained. An application that performs like a calculator -- with a simple GUI that takes input and displays output that is algorithmically derived from the input alone -- is trivial to convert into an applet. (Another self-contained example would be a synthesizer which uses a keyboard as a crude musical instrument.) In this case you need to create a subclass of java.applet.Applet in which you override the init() method to initialize your applet's resources the same way the main() method initializes the app's resources. (init() might be called more than once and should be designed accordingly. Moreover, the top-level Panel needs to be add()ed to the applet in init(); usually it was add()ed to a Frame in main().) That's it!
  2. Now imagine an movie viewer. Such an application depends on external data files. From the applet, such files can be located relative to the HTML file's URL. This is called the document base. This gives the applet limited read-only access to resource files (as opposed to full read/write access to an entire hard drive). Even in the simplest case, such an application might have the ability to run in the background indefinitely, consuming system resources and producing output continually. For the applet, you may want to control that thread so that it uses resources only when the applet is visible. In that case you will also override Applet's start() and stop() methods so that the browser will take care of calling these methods when the applet comes in and out of focus.
  3. Next, picture having copies of your application that others can run. This is where applets provide a key benefit: ease of distribution. To accomplish this, the file-based URL will have to be changed to a URL of the form http://server:portnum/path/page.html. In addition, there will have to be a Web server running at port, and the Web server will have to be accessible from the machine where the applet should be run.

Note that, by the time we reach our final example, the size of the application has become important. A small applet can download in this way quickly over a LAN -- almost as fast as from a local hard disk. But the larger the applet and the slower the network, the more noticeable the delay in loading. The fact that most applications are large and require the use of resources beyond those of the previous examples means that to convert the application to an applet requires significantly more effort. Essentially, it involves splitting the GUI-component from the rest of the application, creating the applet to distribute the GUI, and providing the other required services from the Web server. Servlets are a good server-side complementary Java technology.

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.
Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more