What's New
Dev Tools
Site Map

Converting an Applet to a Standalone Java Application

An applet is designed to run within a browser. In other words, the applet is one component of the larger browser application. To make an applet run as a standalone application requires only a few steps. Essentially, you must create a Java Frame for the applet to sit within (so the applet will sit within the Java Frame, instead of the Browser).

The applet itself doesn't have to be changed to run within a Java Frame. Panels can be placed directly onto Java frames. Since Applet is inherited from Panel, it is "frame-ready".

Different methods for constructing your Java frame and mainline application are explored.

Simple Setup: Frame/MainLine in one Class (separate from Applet)

In the example below, the frame is defined and then the applet is added into the frame all within the application's main method.

import java.awt.*;
public class AppletFrame {
  public static void main(String[] args) {
    TheApplet myApplet = new TheApplet(); // define applet of interest
    Frame myFrame = new Frame("Applet Holder"); // create frame with title
    // Call applet's init method (since Java App does not
    // call it as a browser automatically does)
    // add applet to the frame
    myFrame.add(myApplet, BorderLayout.CENTER);
    myFrame.pack(); // set window to appropriate size (for its elements)
    myFrame.setVisible(true); // usual step to make frame visible
  } // end main
} // end class

In fact, the main method above could simply be added into the applet class itself but the separating the applet and mainline/frame makes it easier to understand.

This simple frame above does not provide a menu or an orderly way to shut down the application.


Adding Menus and Separating Mainline and Frame into Separate Classes

A more usable standalone Java application will include a menu and the ability to shut down the application. Such an application is demonstrated next.

A frame class called MyFrame is defined for the application. In the MyFrame's constructor, a menu is added and the applet is added to the frame. The menu actions are handled in the actionPerformed event. When the user chooses exit, the application is shown down.

import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame implements ActionListener {
  public MyFrame() { // constructor
    super("Hold Applet with Menu"); // define frame title
    // define Menubar
    MenuBar mb = new MenuBar();
    // Define File menu and with Exit menu item
    Menu fileMenu = new Menu("File");
    MenuItem exitMenuItem = new MenuItem("Exit");
    exitMenuItem.addActionListener (this);
    // define the applet and add to the frame
    TheApplet myApplet = new TheApplet();
    add(myApplet, BorderLayout.CENTER);
    // call applet's init method (since it is not
    // automatically called in a Java application)
  } // end constructor
  public void actionPerformed(ActionEvent evt) {
    if (evt.getSource() instanceof MenuItem) {
      String menuLabel = ((MenuItem)evt.getSource()).getLabel();
      if(menuLabel.equals("Exit")) {
        // close application, when exit is selected
      } // end if
    } // end if
  } // end ActionPerformed
} // end class

The only other thing that this standalone application requires is an application mainline. This is defined in a separate class. MyFrame is defined, sized and made visible in the application main method as shown below:

import java.awt.*;
public class AppMain {
  public static void main (String[] args) {
  // define frame, its size and make it visible
  Frame myFrame = new MyFrame();
  myFrame.setBounds(10, 10, 700, 600); // this time use a predefined frame size/position
  } // end main method
} // end class

Limitations in converting Applets to Applications

Generally speaking, an applet can easily be converted to an application if the applet is the independent of the HTML page where it resides. If the applet relies on HTML pages elements and dynamic "LiveConnect" calls to/from JavaScript, then it will require more work to convert the applet to a standalone application (because all the HTML/JavaScript interaction will have to be separately migrated to Java).

... back to Java Topics

Copyright 1997-2019, Woodger Computing Inc.