Applet Notes
-Rupali Shinde
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
1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Java.
2. 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.
1. Under Project Name, type HelloApplet . Change the Project Location to
any folder on your computer.
2. Click Finish. If you imported an applet source file, run it.
Create the applet source file
1. Right-click the HelloApplet project and choose Properties to open the Properties window.
2. Select the desired Source / Binary Format for the project and click OK.
Note: For example, if you choose JDK 6 the applet might not run on machines that have an
older version of the JRE or Java browser plugin.
1. Right-click the HelloApplet project node in the Projects window and select New > Other
(Ctrl-N).
2. Under Categories, select Java. Under File Types, select Applet.
Alternatively, if you want to visually design your applet, select Swing GUI Forms > JApplet
Form.
Click Next.
1. Under Class Name, type MyApplet . Under Package, type [Link] .
2. Click Finish.
The IDE creates the applet source file in the specified package. The applet source file opens in
the Source editor.
1. Define your applet class by copying and pasting the following code over the existing
default code:
package [Link];
import [Link];
import [Link];
public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello applet!", 50, 25);
}
}
Running an Applet Source File
The applet that you created can be run from the IDE. This section shows you how to do so.
Build and run the applet source file
Right-click the [Link] file node in the Projects window and choose Run File from the
contextual menu. The [Link] launcher file, with the applet embedded, is created in the
build folder, which you can see if you switch to the Files window (Ctrl-2):
The applet is launched in the Applet Viewer:
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
1. Choose File > New Project.
2. Select Web Application in the Java Web category. Click Next.
3. Under Project Name, type HelloWebApplet .
4. Change the Project Location to any folder on your computer. Click Next.
5. Select the target server. Click Finish.