Java Java intro
  1. Java intro
  2. Java basics
Java core
  1. Java variables
  2. Java conditionals
  3. Java loops
  4. Java arrays
  5. Java strings
Object-oriented Java
  1. Java OOP 1
  2. Java OOP 2
  3. Java packages
  4. Java interfaces
Java interactive
  1. Java user input
  2. Java exceptions
  3. Java events
Graphical Java
  1. Java GUI
  2. Java GUI layout
  3. Java graphics
Java extras
  1. Java applets
  2. Java sounds
  3. Java random numbers
Java wrap-up
  1. Java summary

Java applets

An applet is a Java program that runs on a webpage. Applets function like regular Java programs but with a few security restrictions (such as applets can't read or write files on your computer).

This tutorial focuses on:

The Applet class

The Applet class is used to create applets. This class is located in the java.applet package. Applets are not created by instantiating an Applet object, rather by creating a class which extends the Applet class.

Example (applet core structure):
import java.applet.Applet; class AnApplet extends Applet{ }

Applet class methods

Building an applet

The first four methods discussed above are actually run by a web browser automatically. You can define the functionality of these methods in your applet code to specify what happens during each stage of an applet's existence.

The applet below uses these methods to print to a textarea what is currently happening.

Applet with functionality:
import java.applet.Applet; import java.awt.TextArea; public class AnApplet extends Applet{ TextArea ta1 = new TextArea(12, 40); //the \n is a 'newline character' //it will start the text string on a new line public void init(){ add(ta1); ta1.append("Applet has been initialized"); } public void start(){ ta1.append("\nApplet has been started"); } public void stop(){ ta1.append("\nApplet has been stopped"); } public void destroy(){ ta1.append("\nApplet has been destroyed"); } }

Placing an applet on a webpage

Now that you have a ready applet. Let's place it on a webpage. This can be done using HTML's <applet> tag.

<html> <head> <title>Applet on a webpage</title> </head> <body> <-- the code attribute denotes the name and location of the applet --> <applet code="AnApplet.class" width="200" height="200"></applet> </body> </html>

Here is the applet:

The applet didn't load!

If the applet doesn't load, try viewing this page in a different web browser.

NOTE: You should always refer to the compiled program file (the one with the .class extension) in an <applet> tag when placing an applet on a webpage. Not the .java source code file!

Viewing an applet

You can view an applet by viewing the webpage on to which the applet is placed in a web browser or you can use the Java appletviewer command line tool. In the command prompt type appletviewer and the name of the page where the applet is located.

appletviewer appletpage.html

The appletviewer will display just the applet, not the entire webpage.

© Copyright 2013-2014
Terms of use | Privacy policy | Copyright information