How do I convert my Java program into an applet?

An applet must be a class that extends java.applet.Applet. The java.applet.Applet class is a JDK 1.1 class that extends java.awt.Panel. A Panel is the AWT equivalent of a javax.swing.JPanel, for the AWT-impaired. So basically when writing an applet back in the old Java v1.1 days, people would write some class that extended Applet and treat it like it was the main panel or content pane of the program. Things would be added to it to be shown on the screen. The "frame" was the web browser window; the applet itself didn't pop up its own frame.

JDK 1.2 and Swing brought us a more powerful Applet class, the JApplet class. A JApplet is closer to a JFrame than an Applet is. You can do new things like adding JMenuBars. Another important difference is that a JApplet has a content pane, while an ordinary Applet essentially is the content pane. So to add things to a JApplet you use getContentPane().add, or setContentPane; while to add things to an ordinary Applet, you use just add. If we're taking the plug-in approach to writing our applets, we can use the "newer" Java classes, so we'll use JApplets.

I will assume that you have a program written with Swing components as its graphical user interface. There are two ways to convert your program into an applet: the easy cheating way, and the correct but slightly more involved way. I will now demonstrate both.

The Cheater Frame Pop-up Applet Conversion

An easy, yet not recommended, way to convert your program into an applet is just to encase the whole program in a trivial applet class.

For example, if you have a Java program whose runnable class looks like:

public class RunMyProgram {
	public static void main(String[] args) {
		new MyFrame().show();
	}
}

Then you can change it into an applet simply by saying:

import java.applet.*;
import javax.swing.*;

public class RunMyApplet extends JApplet {
	public void start() {
		new MyFrame.show();
	}
}

The problem with this solution is that it pops up the applet in its own frame window. It's not embedded in the web page the way an applet is expected to be. But hey, it's an applet, and it works. Pretty quick conversion.

EXAMPLE: Run the AnimationDemoAppletCheat applet, or take a look at its source code.

There are some snags, though. Read on.

 

The Legit Applet Conversion:

A more correct way to convert your program into an applet is to change the JFrame-extending class into a JApplet-extending class. Converting a JFrame into a JApplet can be as easy as changing the class name, or it can be tedious work, depending on what your program does.

The first step in changing your JFrame into a JApplet is to change its constructor into the init method. That's done easily enough.

The second step is to remove all offending lines of code that applets will not allow. A JApplet doesn't have every method that a JFrame has. Some examples of missing methods in JApplet and why they aren't there:

METHOD NAMEREASON FOR OMISSION
setDefaultCloseOperationCan't "close" an applet; only the browser window containing it
setTitleThe applet isn't a window of its own, therefore it has no title
setSize
setPreferredSize
pack
Well, the set...Size methods do exist. But they don't really resize the applet the way you like. The web browser sets the size of the applet, not the applet itself

EXAMPLE: Run the AnimationDemoApplet applet, or take a look at its source code. Note that it doesn't pop up its own window like the "cheat" applet did.

 

PREV