0% found this document useful (0 votes)
4 views7 pages

How To Create and Deploy A Java Applet (Embed in HTML)

This document is a tutorial on creating and deploying a Java Applet using NetBeans IDE 7.1. It is divided into three parts: creating a new Java Applet project, adding functionality to the applet, and embedding the applet in an HTML page for deployment. The tutorial includes step-by-step instructions and necessary tools required for the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

How To Create and Deploy A Java Applet (Embed in HTML)

This document is a tutorial on creating and deploying a Java Applet using NetBeans IDE 7.1. It is divided into three parts: creating a new Java Applet project, adding functionality to the applet, and embedding the applet in an HTML page for deployment. The tutorial includes step-by-step instructions and necessary tools required for the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

www.PhilipStarritt.

com

How to Create and Deploy a Java Applet (Embed in HTML)

This tutorial will explain how to create a simple Java Applet and deploy it on a web browser
such as Google chrome, Firefox & Internet Explorer.

Part 1 - Explains how to create a new Java Applet project using NetBeans IDE 7.1. Download
Part 2 - Design & Source. (Adding a simple Design and Code to the Java Applet) Download
Part 3 - Will show you how to deploy your Java Applet on a web browser. (Embed In
HTML) Download

Things needed:
 Java IDE - (e.g. NetBeans 7.1, Eclipse, JCreator)
 Java JDK (Preferably 6 or 7)
 HTML editor - (e.g. Notepad++)

Please note - I will be using NetBeans Java IDE (v7.1) and Notepad++ (v5.9.6.1) during this
tutorial.

Part 1

Once you have opened NetBeans, select File, New Project. This will open the New Project
Window (Figure 1.0), in this window select Java, Java Application and click Next.

www.PhilipStarritt.com Java Applet Tutorial


Figure 1.0

You must now select your project Name and its location (Figure 1.1). I have called mine
JavaAppletTutorial and used the default values. Deselect Create Main Class.

Figure 1.1

On the left hand side under Projects you will see your projects directory. Right click on
Source Packages and select Java Package. This package will hold your Java Applet. More
information on Java Packages can be found Here.

Figure 1.2

www.PhilipStarritt.com Java Applet Tutorial


In the pop-up window, name your package and everything else should be left as default.
Click Finish.

Figure 1.3

Once your package has been added, right click on it and select JApplet Form from the drop
down menu. (See figure 1.2 again)
A pop-up window will appear prompting you to enter the applets information. Give your
Applet a name and everything else should be left as default. (Make sure the Package is set to
the package you just created)

Figure 1.4
If you have followed these steps correctly, you will have created an empty Java Applet. Part 1
is now Complete.

www.PhilipStarritt.com Java Applet Tutorial


Part 2

Now we need to add some basic functionality to the JApplet. Select Design.

Figure 2.0

Under Swing Controls in the Palette, drag and drop a Button and Label to the JApplet. Give
them a relevant name, some text and colour.

Figure 2.1

It should look something like this:

Figure 2.2

To view your Java Applet in NetBeans 7.1 Applet Viewer, right click on the JApplet Java file
and select 'Run File'. Alternatively hold down Shift and press F6.

www.PhilipStarritt.com Java Applet Tutorial


Figure 2.3

Close the Applet Viewer.

Now we are going to create an event handler for the JButton. Double click on your button or
right click > select Events > select Action > select actionPerformed.

Before we write some simple code inside the event handler declare a boolean variable called
'flag' above the @Override & init() Method. For example:

private boolean flag;


@Override
public void init()
{
setSize(400,120);
flag = false;

//Generated Code
.............................
.............................
}

Now scroll down to the JButton event handler and type the following code inside the method.

public void buttonNameActionPerformed(java.awt.event.ActionEvent evt)


{
if (flag)
{
labelName.setVisible(true);
flag = false;
}
else
{
labelName.setVisible(false);
flag = true;
}
}

www.PhilipStarritt.com Java Applet Tutorial


Save your work and run your JApplet. Click the JButton a few times, does the JLabel
disappear and reappear?

If so, Part 2 is now complete and you are ready to deploy your Java Applet on your Web
Page.

www.PhilipStarritt.com Java Applet Tutorial


Part 3

In this section we will embed a Java Applet in HTML. You will need an HTML editor, Web
Browser and your Java Class Files.

The first thing you should do is create a new folder that will contain your HTML files, class
files and anything else needed. This will be referenced as your Web Folder.

Figure 3.0

We now need to get hold off the JApplet Class files. Locate your project folder and select
build. E.g.(NetBeans Projects > select YourProjectName > select build)
You should now see a folder called classes. Copy this folder and paste it into your Web
folder.

Now create an HTML webpage and save it in the Web folder. A tutorial on HTML webpages
can be found Here. In the Body section write the code:

<applet id="appletHTML" codebase="classes"


code="javaAppletPackage/MyFirstJavaApplet.class" width="400" height="120"> </applet>

Please Note - javaAppletPackage should be the name of your package. (Open classes folder
and there should be another folder, it is that folders name). MyFirstJavaApplet.class should
be the name of your JApplet class.

Now open your HTML Web Page in a Web Browser. You should see your working Java
Applet. Below is the example JApplet created during this tutorial.

www.PhilipStarritt.com Java Applet Tutorial

You might also like