|
Contents
To follow this tutorial, you need the following software and resources.
Creating or Importing an Applet Source File
In this section, you create your first applet. Possibly, you already
have an applet and, in this case, you can use the instructions below to import
it into the IDE.
Create the Java project from scratch or from existing sources
- Choose File > New Project (Ctrl-Shift-N).
Under Categories, select General.
- Choose one of the following:
- If you are creating a new applet source file, select Java Class Library
under Projects. Click Next.
- If you want to import an applet source file, select Java Project with Existing Sources.
Click Next. Specify the file's location in the
Source Packages Folder text box.
- Under Project Name, type HelloApplet. Change the
Project Location to any folder on your computer.
- Click Finish. If you imported an applet source file, run and debug it.
Create the applet source file
- Right-click the HelloApplet project node in the Projects window
and select New File/Folder (Ctrl-N).
- Under Categories, select Java Classes. Under File Types,
select Applet.
Alternatively, if you want to visually design your applet, select Java GUI Forms > AWT Forms > Applet Form.
Click Next.
- Under Class Name, type MyApplet. Under
Package, type org.me.hello.
- Click Finish.
The IDE creates the applet source file in the
specified package. The applet source file opens in the Source editor.
- Define your applet class by copying and pasting the following
code over the existing default code:
package org.me.hello;
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello applet!", 50, 25);
}
}
Alternatively, if you are designing an Applet Form instead, use the Designing a Swing GUI in NetBeans IDE document to create something like the following:

For detailed information on writing applets, see The JavaTM Tutorial's
Applets section.
Running and Debugging an Applet Source File
The applet that you created can be run and debugged from the IDE. This section shows you how to do so.
Build and run the applet source file
- Right-click the HelloApplet project node in the Projects
window and choose Build Project from the contextual menu.
- The HelloApplet.jar file is
created in the dist folder.
- Right-click the applet class node in the Projects window
and choose Run File from the contextual menu.
The MyApplet.html launcher file,
with the applet embedded, is created
in the build folder:

The applet is launched in the Applet Viewer:

Applet forms are also displayed in the Applet Viewer:

Debug the applet source file by modifying its parameters
The MyApplet.html launcher file
in your build folder is overwritten each time
you run or debug the applet. Therefore, do not modify the launcher file
in your build folder. Instead, use the following procedure:
- Open the Files window (Ctrl-2).
- Copy the MyApplet.html launcher file from the build folder
to the package where the applet class lives in the src folder (in this case, to org.me.hello).
Make sure that the MyApplet.html launcher file has the same name as the applet class.
- Now edit
the MyApplet.html launcher file as needed.
When you build the project, the
MyApplet.html launcher file is copied from the src folder to the build
folder.
Tip: The launcher file is created by the IDE when you run
or debug an applet. If you copy it to your src folder for
editing, it will automatically be included in the JAR file when
you build the project. Normally, you do not need to include this file when
you package your application. Exclude the launcher file from the JAR file by
right-clicking the project, choosing Properties, clicking Packaging, and
adding an expression to exclude launcher files such as MyApplet.html.
Embedding an Applet in a Web Application
Your applet is complete. Now you need to make it available
to the user. To do so, you create a web application, put the applet JAR
on its classpath, and then add an applet tag to the web application's HTML file.
Create the web project
- Choose File > New Project.
Under Categories, select Web. Under Projects, select Web Application.
Click Next.
- Under Project Name, type HelloWebApplet.
Change the Project Location to any folder on your computer. Click Finish.
Add the applet JAR file to the web project
When you want to include an applet JAR file in a web project,
you can do so by adding the Java project that contains the JAR file, or by
adding the JAR file itself. Although the choice is yours, note that when you add the
Java project to the web project, you enable the IDE to build
the applet whenever you build the web application. Therefore, when you
modify the applet in the Java project, the IDE builds a new version
of the applet whenever the web project is built. On the other hand, if the applet
JAR file is not in a NetBeans IDE
5.x Java project, the applet source file is not rebuilt when you build the web project.
- In the Projects window, right-click the HelloWebApplet project node and select
Properties from the contextual menu.
- Choose one of the following:
- If the applet JAR file is in a Java project, click Packaging, and then click
Add Project. Browse to and select the folder that contains the Java project.
Note that IDE projects are marked by the NetBeans IDE project icon.
- If the applet JAR file is not in a IDE project, click Packaging, and then click
Add JAR/Folder. Browse to and select the folder that contains the JAR file.
The JAR file containing the applet source file is listed in a table at the bottom of the Project Properties dialog box.
- Optionally, you can type a location for the applet in the table's Path in WAR
column. By default, the applet JAR file
will be copied to the web application's document root, which is the build/web
folder (the highest
level of the web application's file structure). Click OK.
When you build the project, the applet's JAR file
is packaged in the project's WAR file in the dist folder. It is also added to the build/web
folder. For details, see the illustration below.

Create and run the JSP file or HTML file
- Choose one of the following:
- If you want to embed the applet in a JSP file, double-click the default index.jsp file in the Projects window.
This file is created by the IDE when you create a web project. It opens in the Source Editor.
- If you want to embed the applet in an HTML file, right-click the HelloWebApplet project node, and choose New > File/Folder from
the contextual menu. Under Categories, select Web. Under File Types, select HTML. Click Next. Give your
HTML file a name and click Finish.
- Embed the applet in the file by adding the following applet tag anywhere
between the file's <body></body> tags:
<applet code="org.me.hello.MyApplet" archive="HelloApplet.jar"/>
- org.me.hello.MyApplet is the full classname to your applet
- HelloApplet.jar is the JAR file that contains the applet
- Right-click the JSP node or HTML node in the Projects window
and choose Run File from the contextual menu.
The server deploys the JSP file or HTML file in the IDE's default browser.
You should see something
similar to the illustration below:
For applet forms, you should see something similar to the following (only successfully tested under FireFox):

Note that applets run in the virtual machine of the IDE's default web browser.
The IDE uses a different virtual machine and therefore applets are not
included in a web application's debug session. Debug the applet within the Java project, as explained
above.
|
|