Applet Life Cycle in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

An applet in Java is a special type of program that is embedded into the web page in order to generate dynamic content. It is also a class in Java that extends the java.applet.Applet class. Applet life cycle in Java is a process of how an object is created, initiated, ended, and destroyed during its entire cycle of execution in an application. It has five core methods which are, init(), start(), stop(), paint() and destroy(). In Java, their methods are invoked for execution. Apart from the browser, applet also works on the client side thereby having less process time.

Methods of Applet Life Cycle

Whenever an applet class is created, an instance of it gets created thereby allowing us to use all the methods of the class. In applet, there is no need of calling a method explicitly, these are automatically invoked by the browser.

Following are the five methods of applet life cycle in Java: APPLET LIFE CYCLE

Method 1: init()

Syntax

  • This is the first method that is being called
  • All the variables are initialized here
  • This method is only called once during the run time
  • It is generally invoked during the time of initialization

Method 2: start()

Syntax

  • It is called after the init method
  • Used for starting an applet
  • It is also used for restarting an applet in case it is stopped

Method 3: paint()

Syntax

  • This method is used for painting various shapes like squares, rectangles, etc.
  • It has parameter of type graphic class, which enables the feature of painting in the applet.
  • The graphics class parameter contains graphics context which is used for displaying the output of the applet.

Method 4: stop()

Syntax

  • This method is invoked whenever the browser is stopped, minimized, or because of abrupt failure within an application.
  • Generally after the stop method, the start method can be used.
  • It deals with the cleaning of the code, the method is called when the browser leaves the HTML document when the applet is running.

Method 5: destroy()

Syntax

  • Once we are done with the applet work, this method destroys the application and is invoked only once.
  • Once the applet is destroyed, it can’t be restored.
  • It is called when the environment determines that the applet needs to be completely removed from the memory.

How Does Applet Life Cycle Work?

  • The Java plug-in software manages the applet life cycle in Java, which is a Java application executed in a web browser on the client side.
  • Unlike standard Java applications, applets do not have a main() method since they run within the browser environment.
  • Applets are made to fit smoothly into HTML pages, allowing them to bring interactive content directly to web browsers.
  • The init(), start(), stop(), and destroy() methods belong to the java.applet.Applet class and are responsible for managing the applet life cycle in java.
  • The paint() method, responsible for rendering graphics, belongs to the java.awt.Component class.
  • To define a class as an applet in Java, it needs to extend the java.applet.Applet class.
  • When an applet is created, it instantiates an object of the Applet class, allowing access to its methods and functionality.

Syntax of Entire Applet Life Cycle in Java

Code Implementation of Applet Life Cycle in Java

Example 1

Code:

Save this file as MyInteractiveApplet.java on your local machine.

Save this file as MyInteractiveApplet.html.

To compile and execute the program:

  1. Compile the Java file: javac MyInteractiveApplet.java
  2. Open the HTML file using a web browser.

Explanation: This code demonstrates an interactive Java applet with a text field and a button. Users can enter text into the text field and click the button. When the button is pressed, it calls the actionPerformed() method to execute its associated actions, displaying the entered text as a status message. This illustrates how Java applets can interact with user input and respond dynamically.

Example 2

Code:

This code snippet demonstrates an applet named AppletLifeCycle, showcasing its life cycle stages with different messages printed to the console. When the applet is executed, it initializes with a blue background, and messages indicating each life cycle stage (init, start, paint, stop, and destroy) are displayed, providing a clear understanding of applet life cycle in Java.

Learn more about Thread Life Cycle in Java

Conclusion

  1. Applet Purpose: Java applets, embedded in web pages, generate dynamic content and extend java.applet.Applet.
  2. Life Cycle Methods: init(), start(), paint(), stop(), and destroy() manage applet creation, rendering, and destruction.
  3. Automatic Invocation: Browser automatically triggers these methods, simplifying applet execution.
  4. Applet Management: Plug-in software handles applet life cycle in Java within web browsers on client-side.
  5. Implementation: Extend Applet class, define life cycle methods, and create dynamic content for web pages.