View RSS Feed

Java Applet

How to use HTML to display an Applet in browsers

Rate this Entry
by , 12-08-2011 at 11:19 AM (10437 Views)
Applet is a Java based application which is embedded into a web page. Its advantage is to provide rich user interface experience and interaction just like other desktop applications, while still keeping the powerful features of Java like cross-platform, security, multi-threading…
This article will guide you through some basic steps to build a simple applet, use HTML code to display the applet in browsers. In addition, you will learn how to passing parameters and loading external libraries for the applet.

Create a simple applet
The following code creates a straightforward simple applet which has a gray background with a button at the center. When clicking on the button, a popup message says “Hi, welcome!” will appear.

Java Code:
import java.awt.event.*;
import javax.swing.*;

public class SimpleApplet extends JApplet {

    public void init() {
        JButton button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(SimpleApplet.this, "Hi, welcome!");
            }
        });

        add(button, BorderLayout.CENTER);
        setBackground(Color.GRAY);
    }
}
Compile the applet
Applet is a normal Java class so it is compiled as normal as using the javac command:

javac SimpleApplet.java

You should get a compiled class file, SimpleApplet.class.

Embed into web page
To display the applet in a web page, there are three HTML tags that you can use: <applet/>, <embed/> and <object/>. The HTML specification states that the applet tag is deprecated, and that you should use the object tag instead. However, the specification is vague about how browsers should implement the object tag to support Java applets, and browser support is currently inconsistent. Oracle therefore recommends that you continue to use the applet tag as a consistent way to deploy Java applets across browsers on all platforms.
First, you have to create a HTML file, give it a name “MyApplet.html”.
We use the <applet> tag to embed our applet. The code looks pretty simple:

XML Code:
<html>
<head>
    <title>My Simple Applet</title>
</head>
<body>
<div align="center">
    <applet name="SimpleApplet"
        code="SimpleApplet.class"
        width="300" height="300"
    />
</div>
</body>
</html>
Look at the applet tag and its attributes.

  • The attribute name specifies a name for the applet instance which makes it possible for applets on the same page to find (and communicate with) each other.
  • The attribute code specifies either the name of the class file that contains the applet's compiled applet subclass or the path to get the class, including the class file itself.
  • The attributes width and height specify the initial width and height of the applet’s display area.



For detailed specification of the <applet> tag, refer to the references section at the end of this article.

Running the applet
It’s time to test our applet in a browser. Make sure that you put the MyApplet.html file at the same folder as the compiled applet class file, SimpleApplet.class.
Double click on the MyApplet.html file, it should be opened by your default browser. The applet will be displayed quickly and looks like a big button with a caption “Click me!”. Press the button, a message dialog says: “Hi, welcome!”. That’s pretty simple, isn’t it?

Passing parameters
Sometimes we need to pass some information from the HTML page to the applet. This can be done by using the <param/> tag in the HTML and the getParameter() method of the applet.
For example, if you want to put a variable called “firstName” and value is “Tom”, construct the <param> tag as follow:

XML Code:
<param name="firstName" value="Tom"/>
Then put this tag as a sub tag inside the <applet> tag:

XML Code:
    <applet name="SimpleApplet"
        code="SimpleApplet.class"
        width="300" height="300"
    >
        <param name="firstName" value="Tom"/>
    </applet>
If you want to pass more variables, simply add more <param> tags. In our example, we pass two parameters, first name and last name:

XML Code:
        <param name="firstName" value="Tom"/>
        <param name="lastName" value="Eagar"/>
Now on the applet side, we need to read the information passed through HTML’s <param> tag. The JApplet class has a method getParameter(String paramName) which is designed for this purpose:

Java Code:
String firstName = getParameter("firstName");
The applet’s code should looks like this:

Java Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleApplet extends JApplet {

    public void init() {
        final String firstName = getParameter("firstName");
        final String lastName = getParameter("lastName");

        JButton button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                JOptionPane.showMessageDialog(SimpleApplet.this, "Hi, welcome " + firstName + " " + lastName);
            }
        });

        add(button, BorderLayout.CENTER);
        setBackground(Color.GRAY);
    }
}
Compile the applet again and refresh the HTML page. You will see the dialog says “Hi, welcome Tom Eagar”.

Loading external libraries
Sometimes we need to use external java libraries (jar files) for our applet. For example, an applet that uploads files to FTP server may require Apache Common networking library (commons-net-ftp-2.0.jar). The <applet> tag has an attribute that allows pre-loading of jar files while the applet is initializing, it is the archive attribute:

XML Code:
<applet archive=”lib1.jar, lib2.jar, …” />
Multiple jars are separated by comma and the path of jar files must be relative to the applet class file.
The following code demonstrates the usage of archive attribute to load the commons-net-ftp-2.0.jar library:

XML Code:
    <applet name="SimpleApplet"
        code="SimpleApplet.class"
        width="300" height="300"
        archive="commons-net-ftp-2.0.jar"
    >
        <param name="firstName" value="Tom"/>
        <param name="lastName" value="Eagar"/>
    </applet

References:

Submit "How to use HTML to display an Applet in browsers" to Facebook Submit "How to use HTML to display an Applet in browsers" to Digg Submit "How to use HTML to display an Applet in browsers" to del.icio.us Submit "How to use HTML to display an Applet in browsers" to StumbleUpon Submit "How to use HTML to display an Applet in browsers" to Google

Comments