Java Applet Tutorial

A Java Applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When we use a Java technology-enabled browser to view a page that contains an Applet, the Applet's code is transferred to our system and executed by the browser's Java Virtual Machine (JVM).

In this Java Applet tutorial, we will cover the basics of Applets and Swing.

  1. Contents
  2. 1. Introduction to Applets
  3. 1.1. Developing the Java Code
  4. 1.2. Running from a Web browser
  5. 2. Introduction to Swing
  6. 2.1. Swing Components
  7. 2.2. Component Actions
  8. 2.3. Layout Management
  9. 3. Appendix

Introduction to Applets

Applet development involves two basic steps, developing the Java code and running it from a Web browser.

Developing the Java code

To create an Applet, we must create a class that extends JApplet from the Swing package. Technically we could extend Applet from the AWT package, but this tutorial will focus on Swing because it provides a more modern and sophisticated set of graphical user interface (GUI) components.

The class definition for the first Applet we will write is shown below:

public class AppletHelloWorld extends JApplet

When we extend an Applet, we need to implement a method called init. All GUI initialization is performed in this method. GUI components are represented by classes and to use these components we must create instances of them and add them to our Applet. For example to create a text area and add it to our Applet, we could write an init method that looks like this: