How To Create and Deploy A Java Applet (Embed in HTML)
How To Create and Deploy A Java Applet (Embed in HTML)
com
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.
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
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.
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
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.
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:
//Generated Code
.............................
.............................
}
Now scroll down to the JButton event handler and type the following code inside the method.
If so, Part 2 is now complete and you are ready to deploy your Java Applet on your Web
Page.
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:
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.