Applet life cycle in java

All Articles

#java #applet in java # life cycle of applet #how to run applet in java

Applet life cycle in java

 Applet life cycle in java 

Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.


Hierarchy of Applet

Description: hierarchy of applet


As displayed in the above diagram, Applet class extends Panel. Panel class extends Container which is the subclass of Component.

Lifecycle of an Applet

  1. Applet is initialized.
  2. Applet is started.
  3. Applet is painted.
  4. Applet is stopped.
  5. Applet is destroyed.

Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.

java.applet.Applet class:

For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.

  1. public void init(): is used to initialized the Applet. It is invoked only once.
  2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
  3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
  4. public void destroy(): is used to destroy the Applet. It is invoked only once.

java.awt.Component class:

The Component class provides 1 life cycle method of applet.

  1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

How to run an Applet?

There are two ways to run an applet

  1. By html file.
  2. By appletViewer tool (for testing purpose).

Article for Reference 

  1. life cycle of an applet in java
  2. Java applet - Wikipedia
  3. What is the applet life cycle in Java?-Quora