Java Applet Tutorials

Java Applet

Applet is introduce by Sun Microsystems in 1995. An Applet is the special kind of Java programming which typically embedded inside a web page and runs in the context of a browser.

The Applet class is contained in the java.applet package, contain several method that provide detailed control over the execution of your applet.

The Applet class provides the standard interface between the applet and the browser environment, in addition java.applet also define the three main interface

Three main interface In Applet
  1. AppletContext
  2. AudioClip
  3. AppletStub

Since, Java applet is deliver to the user in the form of java's bytecode, so it is platform independent. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer( i.e. a stand-alone tool for testing applets), usually written in java programming language but can also written in another language that enable to compile Java bytecode such as JRuby, Jython or SmartEiffel.

Applet Basics

All the applet's class must implement java.applet package. Applet also import from java.awt package, AWT stand for Abstract Window Toolkit.

Since Applet also stand along application but it don't need main() method for execution and doesn't support system,out,print(); Rather it is handle various AWT method like JButton, JLevel, etc also handle with various location by drawString() method, which outputs a string to a specified X,Y location.

Lifecycle of Applet
  1. init() method- use for initialized and load the applet into applet viewer or browser.
  2. start() method- use for start the applet in applet viewer or browser.
  3. stop() method- use for stop the running applet.
  4. destroy() method- use for final clean up or unload the the applet.


That when webpage is refresh or reload the current instance of applet is stop and destroy( by calling stop() and destroy() method itself), after that a new instance of same applet is generated automatically.Use the following program:

First Applet Program
 * Save as a
 * Program define complete Life cycle of applet.

import java.awt.*;

public class FirstApplet extends java.applet.Applet {

//define a private label
private Label label;

* Initialization method that will be called after the applet is loaded
* into the browser.
public void init() {

public void start() {
* Start method is call after the applet is completely load
* inside a AppletViewer/Browser
label = new Label("R4R Tech Soft! Welcome:");

public void stop() {
//Stop method is call when close button is click

public void destroy() {
//Method call After Stop method for Finally unload the applet

public void paint(Graphics g) {
*Method used for providing Graphic in applet.
* Call at the run time

Embedded Applet in HTML page

Use the following syntax for such operation.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>R4R Tech Soft</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Use the following syntax for Embedded an applet into a HTML page
name= Name of Applet
code=Provide the path of .class file of the applet( not .java file)
height && width= provide the context area of applet in HTML page
<applet name="CommApplet1" code="" height="200" width="200" />

AppletStub Interface

When an applet is first created, an applet stub is attached to it using the applet's setStub method because stub serves as the interface between the applet and the browser or applet viewer in which the application is running. AppletStub interface contain some of method like-

  1. void appletResize(int width, int height): used to when the applet wants to be resized.
  2. getAppletContext(): use to handler the applet's context.
  3. getParameter(String name): use to get the named/string parameter from the HTML/Field tag.
  4. isActive(): use to determines the state of applet.
  5. getCodeBase(): use to gets the URL of applet.
  6. getDocumentBase(): use to gets the URL of the document in which the applet is embedded.
Limitation of Applet

Applet have some limitation such as

  1. Since, applet is the platform independent but for running condition it need Java Virtual machine (JVM) or sun's AppletViewer.
  2. Across internet, it is embedded into HTML page, very bulk process.
  3. Applets executes under security limitations( sandbox security model) that disallow certain operation and divide into two category-trusted and untrusted applet.