Applets in Java: Definition & Example

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

An applet isn't a tiny apple (nor is it related to Apple technology). It's a small Java program that runs on the web. In this lesson, we'll define the Java applet and provide a working code example.

Applet: Small Application (Not a Baby Apple)

A baby owl is an owlet, whereas a baby apple is NOT an applet! A Java applet is a Java program. However, like a baby, it needs some help with some basic functions. It cannot be run on its own: It isn't like Calculator or Word. Instead, it must be run in a Java-enabled web browser.

Unlike standard Java programs, there is no main function in a Java applet. Since it is not being compiled to run on its own, it doesn't need it.

Java also uses a tool called the AWT (Abstract Windowing Toolkit). This allows us to create a graphical user interface. In our example, we'll have a text display and a button. Without AWT, an applet serves little purpose, especially on web pages.

Java is an object-oriented programming language, and applets are no exception. An applet actually inherits variables and methods from a delivered Java class called Applet. Inheritance is huge in object-oriented programming: It means that you don't have to re-create all of the cool functions within the existing class. The first line of code in a Java applet may look like the following:

public class MyCoolApplet extends Applet {

The extends keyword tells Java that you want to inherit from the given class. In this case, it is the delivered Applet class.

How It Works

When you write the Java applet code, you compile your program. The compiler will create a .class file (this contains all of your code), that the web browser can read. This .class file has the same name as the applet you created; e.g., the name of the class. If you named your applet HeyJoe, the .class file will be HeyJoe.class.

Next, you need to create a web page. This is an HTML file (e.g., mycoolapp.html) that contains a link to the Java applet. We'll dig into the code shortly, but the following shows how the browser, web page, and Java applet work together.

Java applet web flow

Sample Applet

Let's take a look at a simple applet. We'll start from the top and work our way down. The very first thing we need is to import some utilities from Java. These are required for some of the things we will be doing in our code. Notice that one of the utilities brings in the AWT (Abstract Windowing Toolkit). We'll need this for the buttons and text we are adding.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

Remember that we need to inherit from the delivered Applet class, so we use the extends keyword. In addition, we are going to have a button on our page and wait for user input, so we need to also make use of an interface that is part of the Applet class. This is done with the keyword implements:

public class MyCoolApplet extends Applet implements ActionListener {

Inside the class, we can declare some variables and screen items. The label, font, and button items come from AWT; we just create new instances of those items to use on our applet:

Label coolText = new Label("");
Font cool = new Font("Segoe UI", Font.BOLD, 16);
Button press = new Button("Click Here for a Message");

Now that we have our form items setup, we can start the main code. But wait, an applet doesn't have a main function! That's right; instead, there is an init() function, which initializes everything. The browser itself will actually start the applet when we navigate to the page.

public void init() {

The final block of code actually carries out the action. When the applet starts, it presents a button on the screen, and just sits there, waiting. Nothing actually happens until the user actually presses the button. If they navigate away from the page and close the browser, the applet stops. However, once the button is pressed, that triggers that listener interface we used (remember the code implements ActionListener?). This is where that comes into play.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account