OverviewStep 1Step 3

Tutorial: Building an applet

Step 2: Generating your source files

The Applet wizard creates a .java file and an applet HTML file and places them in the project you just created with the Project wizard.

To generate these source files for your applet, follow these steps:

  1. Select File|New and double-click the Applet icon in the object gallery to open the Applet wizard.

    Object gallery

  2. Accept the default package name, firstapplet, in Step 1. By default, the wizard takes the package name from the project file name, FirstApplet.jpr.

  3. Enter GoodEveningApplet in the Class field. This is a case-sensitive Java class name.

    Note: The fully qualified class name (package name + class name) is firstapplet.GoodEveningApplet.class. The class file is saved in the following Java package structure: firstapplet/GoodEveningApplet.class.

  4. Change the Base Class to java.applet.Applet.

    Caution: If you create your applet using the default base class, javax.swing.JApplet, your applet won't run in the browsers. Swing is not yet supported by the browsers.

  5. Check Generate Standard Methods.

    Step 1 of the Applet wizard should look like this:

    Applet wizard Step 1

  6. Click Next to go to Step 2. In this step, you can add parameters to your applet. The wizard adds the PARAM tags inside the APPLET tags in the applet's HTML file and also inserts code for handling parameters in the source code. Applet parameters, the equivalent of command-line arguments for applications, allow you to customize your applet. Do not add any parameters.

    See also: "Defining and using parameters" at http://www.java.sun.com/docs/books/tutorial/applet/appletsonly/param.html.

  7. Click Next to go to Step 3 of the wizard.

  8. Make the following changes in Step 3:
    1. Accept the default option Check Generate HTML Page. When you select this option, the HTML file that calls the applet is created.
    2. Uncheck the option Place In Output Directory. When you uncheck this option, the HTML file is saved to the project's source directory specified on the Paths page of the Project Properties dialog box (Project|Project Properties|Paths). When you select this option, the HTML file is saved to the project's output directory.
    3. Title: Good Evening HTML page
      The title displays in the web browser window when the applet is running.
    4. Accept the default values for all other attributes.

    Step 3 of the Applet wizard should look like this:

    Applet wizard Step 3

    Some of the following attributes are found in the APPLET tag of the HTML file:

    CODEBASE
    This optional attribute specifies the path relative to the HTML file that the browser searches to find any necessary class files. A value of "." specifies the same directory as the HTML file running the applet. The CODEBASE attribute is required when the class files are in a different directory than the HTML file.
    CODE
    This required attribute, which is automatically inserted by JBuilder's Applet wizard, is the fully qualified class name (package name + class name) of the applet class that contains the init() method. You'll see it in the HTML file when it is generated.
    ARCHIVE
    This optional attribute, which is not listed in the Applet wizard, is required when the applet is deployed in a JAR, ZIP, or CAB file. Archive files must be in the directory specified by CODEBASE.
    NAME
    This optional attribute names the applet.
    WIDTH/HEIGHT
    These required attributes determine the width and height of the applet display area in pixels.
    HSPACE/VSPACE
    These optional attributes determine the horizontal padding (left and right margins) and vertical padding (top and bottom margins) around the applet in pixels.
    ALIGN
    This optional attribute determines the alignment of the applet on the HTML page.

    Important: The values for CODEBASE, CODE, ARCHIVE, and NAME must be in quotation marks and are case-sensitive.

    See also: "APPLET tag attributes" topic in "Working with applets" in Building Applications with JBuilder.

  9. Select Finish to close the Applet wizard.

    Note that two files are created and added to the project, GoodEveningApplet.java and GoodEveningApplet.html. Double-click each file and select the Source tab in the content pane to view the generated code.

    Look at the .java file and note the following:

    Look at the HTML file and notice that the wizard inserted the CODE value, firstapplet.GoodEveningApplet.class.

  10. Choose File|Save All to save the source files and the project file.

    Note: By default, JBuilder saves the source files to:

    /[home]/jbproject/FirstApplet/src/firstapplet

    In this tutorial, the applet HTML file is also saved to the src directory.

    The class files, after compiling, are saved to: /[home]/jbproject/FirstApplet/classes/firstapplet

    Java always follows the package hierarchy when saving files. In this example, the source and class files are saved within a firstapplet directory on the source and output paths to reflect the firstapplet package structure. These paths are set for the project in the Project Properties dialog box. In this example, we accepted the default JBuilder paths.


Overview Step 2Step 4

Tutorial: Building an applet

Step 3: Compiling and running your applet

Now, compile and run the applet.

Important: For information on running JDK 1.1.x and 1.2 applets in JBuilder, see the "Runtime" topic in "Release Notes" (Help|Release Notes).

  1. Choose Run|Run Project, or click the Run button Run icon to compile and run your applet. By default, the Applet wizard automatically selects the runnable main class and runs the applet in JBuilder's applet viewer (AppletTestbed).

    Tip:You can also right-click GoodEveningApplet.html in the project pane and select Run. This runs your applet in Sun's appletviewer.

    You can change the applet's run settings on the Run page of the Project Properties dialog box. To access this dialog box, select Project|Project Properties or right-click FirstApplet.jpr and select Properties.

    Important: Applets run from the HTML file, which calls the class containing the init() method, not from the .java file. Any attempt to run the .java file results in an error message (unless the Can Run Standalone option was selected in Step 1 of the Applet wizard):

    java.lang.NoSuchMethodError: main 
    Exception in thread "main" 
    
    When you run your applet, the message pane appears at the bottom of the AppBrowser where any compile-time errors are displayed. Correct any errors and run the applet again.

    Your applet is displayed and should look like this:

  2. Choose Exit in the "Good Evening" applet to close it.

  3. Right-click the GoodEveningApplet tab in the message pane and select "Remove GoodEveningApplet Tab" to close any runtime messages.

Overview Step 2 Step 4