Java Applets

Introduction to Java Applet

In Java an Applet is a panel which allows to interact with Java program, it provides the web interface and one can run it in the browser. One of the main reasons for the popularity of Java is this feature. Java Applets run in the virtual machine of the browser. Java Applet is to be an HTML webpage to be run on the browser, but this webpage is of a special kind of web page so that it can tell the browser that the webpage is an Applet.

One of the feature of Java Applets is that it is secure, Java Applet runs in the sandbox, and it doesn’t have access to the client’s file system.

Browser Compatibility
Most of the browsers support Java 1.4 but they need a plugin to be installed on them.

Applet Viewer
Sun provides an appletviewer to view the applets without a browser. It will show the exact same page as it will be displayed on the browser.

Using Java Applets

You can simply extend your class from JApplet, it is just like other classes which you can use in your projects. in the case of Applets, your browser provides the main method, for your Applet.

The Hierarchy of Java Applet

  • java.lang.object
    • java.awt.Component
      • java.awt.Container
        • java.awt.nPael
            • javax.swing.JApplet

The Methods involved in Creating Java Applet

  • public void init ()
    • init() is initially called when the program is loaded, all the member variables are initialized in this method.
  • public void start ()
    • start() is similar to init() as it starts the Applet.
  • public void stop ()
    • stop() is used when the applet is stopped by the browser or the user due to some reason.
  • public void destroy ()
    • When we need to free some space on the disk which is occupied by the Applet, we can destroy the Applet by using destroy(). and when the Applet is no longer needed we use destroy().
  • public void paint (Graphics)
    • paint() is used to draw any graphical element on the screen, in your Applet. Another similar method repaint is used to refresh the Applet.

Java Applet Life Cycle

init() -> start() -> stop() -> destroy()


Java Applet Life Cycle
Java Applet Life Cycle

You Can’t do with Java Applets

  1. Read data from clients hard drive.
  2. Write Data to Clients hard drive.
  3. Open network connections other than the connection from which the applet was initially loaded.
  4. Link client side code and can’t use client side installations.
  5. Read any other data from the client’s computer.

Java Applet Example

To make Java Applets understandable you need to understand this Hello World Applet.

  1. You need to import all the awt components along with JApplet from swing in the project.
  2. Create a class HelloWorldApplet and Extend HelloWorldApplet from JApplet.
  3. Implement the paint method and pass an instance of Graphics in it as parameters.
  4. As the Applets don’t have a main method so the complete code will be like.

  5. Output
    Hello World to Java Applet
    Hello World to Java Applet

    Running a Java Applet

Before you run your Java Applet there are some changes you should make in the configurations of your project properties.

Right Click on project and open Properties

Under Application -> Web Start
Check Enable Web Start
Choose Applet descriptor from radio buttons.
– Press Ok to save changes.

Right Click on the Applet Java file and click Run to run the Applet in Applet Viewer.

Configuring Java Applet Properties in Netbeans
Configuring Java Applet Properties in Netbeans