Combining Applets and Applications

An applet needs a frame to run in. Normally the web browser or the applet viewer provides this. However you can create instances of your applet inside a frame of your own creation. This way you can write code that is both an applet and an application.

To convert an applet into an application, add the following main() method to your applet:

 public static void main(String args[]) {
    
  myApplet a = new myApplet();
  a.init();
  a.start();
  
  Frame appletFrame = new Frame("Applet Window");
  appletFrame.add("Center", a);
  appletFrame.setSize(150,150);
  appletFrame.setLocation(100,100);
  appletFrame.show();
 
}
Line 1 is the standard main() method you're used to from all the command line applications. If the applet is running in a web browser or an applet viewer, this method will not be called. It will only be executed if you start the applet as a stand-alone program.

Line 3 creates a new instance of the applet. This assumes that the applet is called myApplet. You should of course change that to match the name of your applet subclass.

After you create the applet, lines 4 and 5 call the applet's init() and start() methods. Normally the web browser or applet viewer does this for you, but you're not running inside such a program so you need to do it yourself.

After the applet has been created, it's necessary to create a Frame to hold it. Line 7 does this with the normal Frame() constructor. You can change the title of the Frame to suit your application.

Line 8 adds the applet to the Frame. Since the default LayoutManager for a Frame is BorderLayout you add it to the center. Remember that java.applet.Applet is a subclass of java.awt.Component so adding an applet to a Frame is kosher.

Line 9 resizes the Frame. Here the size is arbitrarily set to 150 pixels by 150 pixels. If this program were running as an applet, you'd get those numbers from the height and width parameters; but you're not running in an applet so you have to make something up. If you like you could make it possible to enter the height and width as command line arguments.

Line 10 moves the Frame to (100, 100). If you don't do this the exact location of the Frame is unpredictable, but on some machines it has a distressing tendency to show up not only partially off the screen, but with the title bar completely off the screen so there's no way to move it onto the screen.

Line 11 makes the Frame visible, and the applet is now ready to run, without an applet viewer or a web browser.

Warning: Even with a Frame applets and applications are still different. When you convert an applet to an application in this fashion, you need to make sure your program doesn't rely on methods that only make sense in the context of an applet. For instance you can only read parameters using getParameter() in an applet. Conversely you can only read the command line arguments in an application. Furthermore applications have many fewer security restrictions than applets so code that may run well in an application may throw many security related exceptions in an applet.


Previous | Next | Top
Last Modified July 1, 1998
Copyright 1997, 1998 Elliotte Rusty Harold
elharo@metalab.unc.edu