Navigate User Interface topic:v  d  e )

A Java applet is an applet delivered in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Oracle's AppletViewer, a stand alone tool to test applets. Java applets were introduced in the first version of the Java language in 1995. Java applets are usually written in the Java programming language but they can also be written in other languages that compile to Java bytecode such as Jython.

Applets are used to provide interactive features to web applications that cannot be provided by HTML. Since Java's bytecode is platform independent, Java applets can be executed by browsers for many platforms, including Windows, Unix, Mac OS and Linux. There are open source tools like applet2app which can be used to convert an applet to a stand alone Java application/windows executable. This has the advantage of running a Java applet in off-line mode without the need for Internet browser software.

The Java applet is less and less used. In 2018, you can use it only from Microsoft Edge's "Compatibility mode" and Firefox Extended Support Release. You'd rather use JavaScript when it is possible.

First applet


The two things you must at least create is an HTML page and a Java class. It can be done on a local folder, no need to run a server but it will be harder to understand what is local, what is remote. The HTML page has to call the Java class using the <applet/> markup:

  Code listing 9.3: HelloWorld.html
<!DOCTYPE html>
    HTML content before the applet.<applet code="HelloWorld" height="40" width="200"></applet>HTML content after the applet.

Save this file on a folder. As the <applet/> markup is calling a Java class called HelloWorld, our class should be called

  Code listing 9.4:
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet {

     * Print a message on the screen.
    public void paint(Graphics g) {
        g.drawString("Hello, world!", 20, 10);

Save this file and compile the class on the same folder. Now let's open the web page on a browser:

Figure 9.8: Java applet HelloWorld.

We clearly see that "Hello, world!" is not rendered the same way as the rest of the page.

HTML code

See also applet markup.

To embed an applet in a HTML page, you have to insert a <applet/> markup. This markup can have several attributes:

code* The name of the main class to call. It could be the name of the class with or without the .class .
height The height of the area where the content of the applet can be rendered on the web page.
width The width of the area where the content of the applet can be rendered on the web page.
archive The name of a compressed zip archive having .jar extension. The archive can contain all the needed classes to run the applet. Applets are usually delivered in this form, to minimize the download time.

The attributes with * are mandatory.

There have been some discussions about the usage of applet tag but it still can be used for beginning and also would work in the real world as well.

Java source code


Applets are not constructed in the same way as other classes or main programs. The entry point is different and the main class should extend the Applet class. The Applet class has four methods that can be called by the browser and you can redefine:

init() Called when the browser first loads the applet. It is only called once by browser execution.
start() Called when the applet starts running. It is called as many times as the user visits the web page.
stop() Called when the applet stops running. It is called as many times as the user visits the web page.
destroy() Called when the user quits the browser. It is only called once by browser execution.
paint() Called when the applet needs to be rendered, for example, when the browser is resized.

The four first methods define the lifecycle of an applet. At least init() or paint() must be redefined. The HTML applet tag can be embedded in the applet source code to allow the applet to be run directly by a simple applet viewer, without the need for an .html file. Typically, the applet tag immediately follows the import statements. It must be enclosed by /* */ comments:

  Code section 9.10: MyApplet comment
 <applet code="MyApplet.class"> </applet>