In this article, you will learn Applet in Java Programming Language. This will cover hierarchy of applet class, lifecycle of an applet, methods of applet class, running an applet, event handling and applets security model.
A Java Applet is a program or an application that contains java codes embedded in an html page on a website. It is used to provide dynamic features to web applications. It fulfils all the interactive features that a webpage lacks being coded in html.
HIERARCHY OF APPLET CLASS
A Java Applet is an extension of the class java.applet.Applet or javax.swing.JApplet(Swing Applet). We have shown below the hierarchical order of the applet class which includes its superclasses and a subclass.

Clearly, applet class extends panel, panel extends container, container extends component and component extends object. JApplet is the subclass of the Applet class.
Unlike other java application, an applet does not have the main() method defined by the Applet Class.
LIFECYCLE OF AN APPLET
In java programming language, when an applet is loaded, it goes through the following stages which is controlled by a java plug-in software.
(1) Instance of an applet subclass is created- This is the first step that an applet undergoes when it is loaded in a web browser.
(2) Applet initializes itself- To initialize the applet, public void init() method is used and is invoked only once in an applet lifecycle.
(3) Applet starts running- To start the applet, public void start() method is used and is invoked after the init() method.
(4) Applet paints itself- To paint or to provide graphics to an applet, public void paint(Graphics gs) method is used. This method can be used to draw shapes such as rectangle, arc, oval etc. It is invoked after the start() method.
(5) Applet stops running- To stop an applet, public void stop() method is used. It is invoked in the following situations
- when the user reload or refresh the page in the browser.
- when the user goes to another page(leaves the page) in the browser.
- when the user quits the browser.
(6) Applet destroys itself- To destroy an applet, public void destroy() method is used. It is invoked when the user reload the page, leaves the page, or quits the browser just like in case of stop() method. A final cleanup is also made by the applet before exiting the browser.
If the user returns back to a page, a new instance of the applet is created by the browser which means the applet’s state is not preserved.
Every time you return to a page or reload a page, the browser will create a new instance of the applet as the current state of the applet is stopped and destroyed.
Except the paint() method, which is provided by the java.awt.Component class, all the above methods are provided by the java.applet.Applet class.
We have shown below an example which demonstrates the life cycle that an applet goes through.
Example:
import java.applet.Applet ;
import java.awt.Graphics ;
/*
<applet code=”AppletLifeCycle.class” width=250 height=200>
</applet>
*/
public class AppletLifeCycle extends Applet {
String result = “” ;
String event ;
public void init() {
event = “Initializing the Applet” ;
printResult() ;
}
public void start() {
event = “Starting the Applet” ;
printResult() ;
}
public void stop() {
event = “Stopping the Applet” ;
printResult() ;
}
public void destroy() {
event = “Destroying the Applet” ;
printResult() ;
}
private void printResult() {
System.out.println(event) ;
result += event ;
repaint() ;
}
public void paint(Graphics gs) {
gs.drawString(result, 50, 50) ;
}
}
Compiling and Running the above Applet Code:
c:\>javac AppletLifeCycle.java
c:\>appletviewer AppletLifeCycle.java
METHODS OF APPLET CLASS
The applet class contains the following methods to create and manage the applet.
- void init()
- void start()
- void stop()
- void destroy()
- void showStatus(String msg)
- voidplay(URL url)
- void play(URL url, String name)
- void setStub(AppletStub stub)
- void resize(Dimension d)
- void resize(int width, int height)
- boolean isActive()
- boolean isValidateRoot()
- String getAppletInfo()
- String getParameter(String name)
- String[][] getParameterInfo()
- static AudioClip newAudioClip(URL url)
- AudioClip getAudioClip(URL url)
- AudioClip getAudioClip(URL url, String name)
- URL getCodeBase()
- URL getDocumentBase()
- Image getImage(URL url)
- Image getImage(URL url, String name)
- AccessibleContext getAccessibleContext()
- AppletContext getAppletContext()
- Locale getLocale()
RUNNING AN APPLET
An applet can be run in the following two ways.
- By embedding the applet codes into an html file.
- By using AppletViewer tool.
[1] BY EMBEDDING THE APPLET CODES
The following steps are involved to run an applet by embedding the applet codes in an html file.
- create an applet in java
- compile the applet codes.
- create an html file and embed the applet codes into html file by using applet tag.
- open the html file to view the applet.
Example:
Step1 : Creating a Java Applet
//AppletEx.java
import java.applet.Applet ;
import java.awt.Graphics ;
public class AppletEx extends Applet {
public void paint(Graphics gs) {
gs.drawString(“This is an Applet”, 65, 100) ;
}
}
Step2: Compiling the Java Codes
c:\> javac AppletEx.java
Step3: Creating an html file and embedding Applet Codes
newapplet.html
<html>
<body>
<applet code=”AppletEx.class” width=“250” height=“200”>
</applet>
</body>
</html>
Step4: Open the html file
After you have embedded the applet codes into an html file, you can now open it to view the applet on the webpage.
[2] BY USING APPLETVIEWER TOOL
The following steps are involved to run an applet using AppletViewer Tool.
- create an applet in java containing applet tag in comments
- compile the applet codes
- run the applet codes
Example:
Step1: Creating an Applet containing Applet Tag in comments
//AppletEx.java
import java.applet.Applet ;
import java.awt.Graphics ;
/*
<applet code=”AppletEx.class” width=”250″ height=”200″>
</applet>
*/
public class AppletEx extends Applet {
public void paint(Graphics gs) {
gs.drawString(“This is an Applet”, 250, 200) ;
}
}
Step2: Compiling the Applet Codes
c:\>javac AppletEx.java
Step3: Running the Applet Codes
c:\>appletviewer AppletEx.java
APPLET EVENT HANDLING
As the name suggests, Event Handling is the process of handling the events. An event is said to be occurred when there is a change in the state of an object.
This change occurs due to the interaction of a user with the components of Graphical User Interface. For instance, clicking a button, scrolling a page, entering a character in a textbox through keyboard etc.
In java programming language, Delegation Event Model is used to handle the events which has the following two components.
- Source- It is an object due to which an event occurs.
- Listener- It is an object that listens or responds to the event. It is also known as Event Handler. The source provides information to the listener about the occurrence of an event. After receiving the information, listener processes the event and returns.
We have shown below a Button example to handle the action event by implementing ActionListener Interface.
Example:
import java.applet.Applet ;
import java.awt.Button ;
import java.awt.Graphics ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
public class EventHandlingEx extends Applet implements ActionListener {
String actionMsg = “” ;
public void init() {
Button button1 = new Button(“Yes”) ;
Button button2 = new Button(“No”) ;
add(button1) ;
add(button2) ;
button1.addActionListener(this) ;
button2.addActionListener(this) ;
}
public void paint(Graphics gs) {
gs.drawString(actionMsg, 15, 50) ;
}
public void actionPerformed(ActionEvent aev) {
String action = aev.getActionCommand() ;
if(action.equals(“Yes”))
actionMsg = “Yes Button Clicked” ;
else if(action.equals(“No”))
actionMsg = “No Button Clicked” ;
repaint() ;
}
}
Invoking the Applet:
To embed the applet into Html, the following codes are used.
<html>
<title>Event Handling in Applet</title>
<hr>
<applet code = “EventHandlingEx.class” width = “300” height = “300”>
</applet>
<hr>
</html>
Also Read:- Generic Methods and Classes in Java Programming Language
We have provided you the description on Applet in Java Programming Language. Hope you like this post. For more updates and related information, stay connected to our blogs on Java Programming Language.