If you are new to Java applet, this tutorial let you quickly get started with a simple applet from writing code and packaging jar file to embedding in HTML page and running in a browser.

 

1. Code a Java applet

A Java applet class must extends from java.applet.Appletclass or javax.swing.JApplet class and overrides applet’s life cycle methods: init(),start(), stop() and destroy(). Following is code of a simple Java applet that displays only a button “Click me!”. On clicking the button, a message dialog says “Hello! I am an applet!” appears.

package net.codejava.applet;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class QuickApplet extends JApplet {

	private JButton  button;

	public void init() {
		button = new JButton("Click me!");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				JOptionPane.showMessageDialog(QuickApplet.this,
					"Hello! I am an applet!");
			}
		});

		System.out.println("APPLET IS INITIALIZED");
	}

	public void start() {
		getContentPane().add(button);
		System.out.println("APPLET IS STARTED");
	}

	public void stop() {
		System.out.println("APPLET IS STOPPED");
	}

	public void destroy() {
		System.out.println("APPLET IS DESTROYED");
	}
}
Place the QuickApplet.java file under the directory structure: src\net\codejava\applet

 

2. Compile and package Java applet

Create a directory called build in the same directory as the src directory. Suppose the current working directory is the parent of the directories build and src, type the following command to compile the applet:

javac -d build src\net\codejava\applet\QuickApplet.java

The compiled .class files will be placed under the build directory. Type the following command to package the applet (not that there is a period at the end):

jar cvf QuickApplet.jar -C build .

That will add all classes in the build directory to a single jar file named QuickApplet.jar.

 

3. Embed Java applet in HTML page



Now create an HTML file called QuickApplet.html with the following code:

<html>
<head>
	<title>Quick Start Java Applet</title>
</head>
<body>
	<center>
		<applet
			code="net.codejava.applet.QuickApplet.class"
			archive="QuickApplet.jar"
			width="125" height="125"
			>
		</applet>
	</center>
</body>
</html>
We use to embed the applet in the HTML page. See the article How to show Java applet in HTML page for more details about using the <applet> tag.

 

4. Run Java applet in browser

Open the QuickApplet.html file in a Java Plug-in enabled browser:

QuickApplet

Click on the button, a message dialog appears:

Applet message dialog

Note that a dialog message created by Java applet has an exclamation mark in the upper right corner.

And following is logging information from Java applet console:

Java applet console

Download the quick applet project in the attachment section.

 

Other Java Applet Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (QuickApplet.zip)QuickApplet.zip[ ]5 kB

Add comment

   


Comments 

#1datadev2019-11-21 11:42
HI Nam
thank you for this article
i built my first java applet

thanks a lot
datadev from paris
Quote