Java Programming Tutorial Index

All of the preceding examples you have learned so far are console based applications. However, these types of applications comprise of only one class of Java program. There is another type of Java program, which is the applet. These are small applications that are accessed on an internet server, transported over the internet, automatically installed and run as part of a web document. In this chapter you will learn about how applet programming is done and what are is special characteristics.

What Are Java Applets?

Applets are small Internet-based program written in Java, a programming language for the Web and can be downloaded by any computer. The applet is also capable of running in HTML. The applet is usually embedded in an HTML page on a Web site and can be executed from within a browser. After an applet arrives on the client, it has limited access to resources so that it can produce a graphical user interface and run complex computations without introducing the risks of viruses or data security breaching.

This applet begins with two import statements. The first import statement is for the Abstract Window Toolkit (AWT) classes. Applets interact with the user through the AWT and not through the console-based I/O classes. An applet must be a subclass of the java.applet.Applet class. The Applet class provides the standard interface between the applet and the browser environment. Swing provides a special subclass of the Applet class called javax.swing.JApplet. The JApplet class should be used for all applets that use Swing components to construct their graphical user interfaces (GUIs). The browser's Java Plug-in software manages the lifecycle of an Applet. The applet in Java can appear in a frame of the web page, a new application window, Sun's AppletViewer, or a stand-alone tool for testing them. They were introduced in the first version of the Java language, which was introduced in the year 1995.

Java applets have been mentioned here for information purposes, but you also need to know that Java applets are deprecated from Java in 2017 and have been completely removed from Java SE 11 (18.9) released in September 2018.

Benefits of Java Applets

  • They are very secure.
  • It works at client side so less response time.
  • Applets can be executed by browsers running under different platforms.

One disadvantage of Applets is that plugins are required at the client browser for executing applets.

Life Cycle of Java Applets

An applet undergoes various stages between its creation of objects and object removal as the work of the applet will get done. This cycle is known as Applet life cycle, where each state is signified by methods. In the life of execution, the applet exists (lives) in one of these 5 states. All of these methods have a name and they are called as callback methods. These methods are named so because they are called automatically by the browser when required for smooth execution of the applet. Here, programmers write the above-mentioned methods with some code but never calls. Following are the methods for a full applet cycle.

  • init() method
  • start() method
  • paint() method
  • stop() method
  • destroy() method

Browser Responsibilities for Applet Life Cycle

Applet life cycle methods are callback methods because they are called implicitly by the browser for the smooth execution of the applet. The browser should provide an environment known as a container for the execution of the applet. Following are the responsibilities of the browser.

  • For the smooth execution, it should call the callback methods at appropriate times.
  • It is responsible to maintain the Applet Life Cycle.
  • It should have the capability to communicate between applets, applet to JavaScript and HTML, applet to browser, etc.

Here is a simple Program to demonstrate Applet in Java:


import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello World!", 50, 25);

Now you have to create an HTML File that Includes the Applet.

Using a text editor, create a file named Hello.html in the same directory that contains HelloWorld.class. This HTML file should contain the following text:



    <TITLE> A Simple Program </TITLE>

    Here is the output of my program:
    <applet code="HelloWorld.class" width="150" height="25"></applet>


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram