Java Notes

Applets

Why no applets in these notes

These notes were originally written using applets almost entirely. However, all applet examples are being changed to applications.

Converting Applets to Applications

To make an applet into an application, add a main()method to the applet's class -- you don't have to create a new class. It may seem a little strange that adding a main method to a class will make it into an application, but that's the most common way of doing it. An application is any program that has a main() method. This new main() does what a browser does.

The main method should do the following:

  1. Create a window (JFrame) to hold the applet.
  2. Make the window's close box stop the applet.
  3. Create a new applet object, and add it to the window.
  4. Start the applet by calling init(), then start().
  5. Finalize the layout.
  6. Make the window (with the applet in it) visible.

Example

Here is a very small applet, which just displays some text.

import javax.swing.*;

public class SampleApplet extends JApplet {
    public SampleApplet() {
        add(new JLabel("This is an Applet."));
    }
}

Add a main() method, and it can be used as either an application or an applet.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
// File   : deployment/appletapp/SampleApplet.java
// Purpose: Demos dual applet / application.
// Author : Fred Swartz - 2006 Aug 5 - placed in public domain.
// Comments: Unless the applet references parameters in the HTML
//           page, just add a main program which creates a
//           JFrame to put the applet in.
//           Applets are often initialized by calling their init()
//           method -- this sample applet defines a constructor 
//           instead of init(), but both styles work.
// Tag: <applet code="appletapp.SampleApplet.class" archive="appletapp.jar"
//                                         width="220" height="20"></applet>


package appletapp;

import java.awt.*;
import javax.swing.*;

public class SampleApplet extends JApplet {
    
    //============================================================ main
    public static void main(String[] args) {
        //... Create an initialize the applet.
        JApplet theApplet = new SampleApplet();
        //theApplet.init();         // Needed if overridden in applet
        //theApplet.start();        // Needed if overridden in applet
        
        //... Create a window (JFrame) and make applet the content pane.
        JFrame window = new JFrame("Sample Applet and Application");
        window.setContentPane(theApplet);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();              // Arrange the components.
        //System.out.println(theApplet.getSize());
        window.setVisible(true);    // Make the window visible.
    }
    
    //=============================================== Applet constructor
    public SampleApplet() {
        add(new JLabel("This is both an Applet and Application!"));
    }
}

Some applets may use features that require additional work.

The <applet...> HTML tag

You need to specify the following values in an applet tag.

Example tag

This applet tag can be used to display the above applet.

<applet code="appletapp.SampleApplet.class" archive="appletapp.jar" width="220"
                                                                height="20"></applet>