Q

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? 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

Dig deeper on Java Web Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close