Ask the Expert

What is a Java Applet?

I still don't understand what a Java Applet is and what it's used for? Would you explain a bit more in layman's terms?

    Requires Free Membership to View

A Java applet is a Java application built to execute within the Java applet framework. The applet framework provides a very restricted "sandbox" in which the applet will run. The framework restricts such things as writing to the user's file-system, accessing servers outside of the applets context, etc. The applet framework mandates that the applet implement a few coarse-grained, lifecycle, callback methods (init, start, stop, and destroy) in order for the applet to perform in a well-behaved manner when running in a Web browser.

Here is an example of a simple applet:

import java.applet.*;
import java.awt.*;
    
public class HelloWorldApplet extends Applet
{
   public void init()
   {
      // Called once when the applet is created.
   }
    
   public void start()
   {
      // Called whenever the applet's container (browser page)
      // gains the focus.
   }
    
   public void stop()
   {
      // Called whenever the applet's container (browser page)
      // loses the focus.
   }
    
   public void destroy()
   {
      // Called once just prior to the applet being destroyed.
   }
    
   public void paint(Graphics g)
   {
      // Called whenever the applet needs to repaint.
      g.setColor(Color.black);
      g.drawString("Hello world!", 4, 50);
   }
}

This was first published in October 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: