Creating and Deploying Applets


NetBeans IDE 5.0 does not have a specific project type for applets, so the development cycle for applets is a little different from that for other types of projects.

You cannot designate an applet as a main project, which means that several project-specific commands (such as Run Project) do not apply to applets.

However, you can still create, test, and deploy applets fairly easily. The general outline of applet development is as follows:

1.

Create a Java Library project to hold the applet.

2.

Create an applet from one of the templates in the New File wizard and fill in code for the applet. The different templates are described below in Creating an Applet.

3.

Test the applet in the JDK's applet viewer by right-clicking the applet's node in the Projects window and choosing Run File.

4.

Create a JAR file for the applet by right-clicking the applet's project node and choosing Build Project.

5.

If you want to add the applet to a web application, add the applet's project (or just the applet's JAR file) through the web project's Project Properties dialog box (Build | Packaging panel).

Creating an Applet

To create an applet:

  1. Choose New Project, select the General category, select the Java Library template, and click Next.

  2. Enter a name and location for the project, and click Finish to exit the wizard.

  3. In the Projects window, expand the node for the project you have just created. Then right-click the Source Packages node and choose New | File/Folder.

  4. In the New File wizard, select one of the available applet templates. There are four available:

    Java Classes category, JApplet template. This template extends javax.swing.JApplet and is recommended over the Applet template, which is based on the less flexible java.applet.Applet class.

    Java GUI Forms category, JApplet template. This template extends javax.swing.JApplet and enables you to use the IDE's Form Editor to design your applet visually. This template is recommended over the JApplet template in the AWT Forms subcategory.

    Java Classes category, Applet template. This template extends java.applet.Applet.

    Java GUI Forms | AWT Forms category, Applet template. This template extends java.applet.Applet and enables you to use the IDE's Form Editor to design your applet visually.

  5. Click Next, specify a name and a package for the applet, and then click Finish.

You can then code the applet, either by hand or with the assistance of the Form Editor.

Running and Debugging an Applet in the Applet Viewer

As you are developing the applet, you can use the JDK's applet viewer to test the applet's functionality. When you use the Run File and Debug File commands, the applet is automatically displayed in the applet viewer.

To run an applet, right-click the applet's node in the Projects window and choose Run File.

To start debugging an applet:

  1. Set a breakpoint in the code by selecting the line where y2u first want execution to pause and press Ctrl-F8.

  2. Right-click the applet's node in the Projects window and choose Debug File.

Running an Applet in a Web Browser

If you want to see how your applet behaves in an actual web browser, you can open an HTML launcher for the applet.

To run an applet in a web browser:

  1. Open the Files window and expand the project's build directory.

  2. Right-click the HTML launcher file (it should have the same name as the applet class but with an HTML extension) and choose View.

The applet opens in the default web browser specified in the IDE. See Changing the IDE's Default Web Browser later in this chapter if you would like to change the IDE's default web browser.

If you want to customize the HTML launcher file, you can copy the generated launcher file into the folder that contains the applet source file. This prevents the launcher file from being overwritten every time you run the applet.

When you run the applet or build the applet's project, the HTML file is copied into the folder with the compiled applet class. If you do not want this file to be included in the JAR that is created when you build the project, you can modify the filter for the JAR file's contents. In the Projects window, right-click the project's node and choose Properties. In the dialog box, select the Packaging node and modify the regular expression in the Exclude From JAR File field. For example, you could add a comma plus an expression like **/Myapplet.html to make sure that your launcher file (no matter which directory it is in) is excluded from the built JAR file.


Packaging an Applet into a JAR File

If you want to put an applet into a JAR file, you can do so by right-clicking the applet's project node in the Projects window and choosing Build Project.

The applet is compiled, and the compiled class files are placed in a JAR file in the dist folder, which you can view with the Files window.

Packaging an Applet into a WAR File

To add an applet to a web application:

  1. Put the applet into a JAR file. See Packaging an Applet into a JAR File above for information on how to do this in the IDE.

  2. Right-click the web application's project node in the Projects window and choose Properties.

  3. In the Project Properties dialog box, select the Build | Packaging node.

  4. Click the Add Project button, navigate to the applet's project folder, and click Add Project JAR Files.

Setting Applet Permissions

When you create and run an applet through the IDE, an applet.policy file is created with all permissions granted and is placed in the root folder of the project (which you can view through the Files window). You can modify this file by double-clicking its node to open it in the Source Editor.

You can also specify a different policy file for the applet. To specify a different policy file:

  1. Right-click the web application's project node in the Projects window and choose Properties.

  2. In the Project Properties dialog box, select the Run node.

  3. In the VM Options field, modify the value of the -Djava.security.policy option to point to the policy file.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net