Composing an Applet

Applets are programs that can be downloaded and run by a Java-enabled web browser. These programs are generally small and specialized. An applet runs in a web page on a client system, within bounds specified by the page markup. A Java applet operates within constraints that provide security from remote system intrusion.

You can compose and test an applet in the Visual Composition Editor. To run an applet in a web page, export the applet class and edit the web page source file to include the applet.

VisualAge provides applet beans from the com.sun.java.swing and java.applet packages, as well as others. Although Swing and AWT beans can be mixed, it is inadvisable.

  1. Create one of the following applet beans:


    Bean Description
    JApplet or Applet A program that can run in a compatible web browser

    Creating an applet bean
    Create an applet as a new composite bean. You can create the bean in any of the following ways:

    • From the Quick Start window. Select Quick Start from the File pull-down menu to open the Quick Start window. Select Basic in the left pane, Create Applet in the right pane, and the OK button to open the SmartGuide - Create Applet window. See the related task topic on Creating an applet for details.

    • From the Create Applet tool bar button. Select the Create Applet button on the tool bar to open the SmartGuide - Create Applet window. Specify one of the applet beans as the superclass for your applet, and request to compose the class visually. See the related task topic on Creating an applet for details.

    • From the Create Class tool bar button. Select the Create Class button on the tool bar to open the SmartGuide - Create Class window. Specify one of the applet beans as the superclass for your applet, and request to compose the class visually. See the related task topic on Creating a class for details.

  2. Compose the user interface and logic for the applet. Add and arrange visual components, add nonvisual beans, and connect them to establish functional relationships.

    Accessing a JApplet bean in the Visual Composition Editor
    When you create a JApplet bean, a content pane is also added in which to place other components. With the exception of a JMenuBar, user interface components are added to the content pane. As a result, the content pane completely covers the Japplet bean in the Visual Composition Editor. To access the JApplet bean, open the Beans List.

    Resizing or moving a JApplet bean in the Visual Composition Editor
    Select the JApplet bean in the Beans List. Then, you can resize or move the JApplet in the Visual Composition Editor. If you try to select the JApplet bean in the Visual Composition Editor, you will select its content pane instead.

    Replacing the content pane for a JApplet bean
    To replace the content pane, delete it and add another container component. When you delete the content pane, a warning is displayed indicating that the content pane is missing. If you save the bean without adding another content pane, a JPanel bean is used.

    Arranging beans in an applet
    Use either of the following methods:
    • Use a layout manager to control size and position of beans within the applet or applet content pane.
    • Without using a layout manager, place beans approximately where you want them and use visual composition tools to align them.

    Accessing the applet context
    The applet context represents the environment in which an applet is running. It provides methods to get an image from a URL, to get an audio clip from a URL, to find other applets within the document, and to show a document at another URL. To access these applet context methods, tear off the appletContext property of the Applet or JApplet bean. Access a JApplet bean in the Beans List.

    Accessing the document or applet URL
    To get the URL of the HTML file that the applet is running in, connect to the documentBase property or the getDocumentBase method of the Applet or JApplet bean. To get the URL of the applet, connect to the codeBase property or the getCodeBase method of the Applet or JApplet bean.

    Providing information about the applet
    To define information about your applet, edit the getAppletInfo method on the Methods page. To get the applet information for an About dialog, connect to the appletInfo property or the getAppletInfo method of the Applet or JApplet bean.

  3. Test the applet. You can run the applet from the Visual Composition Editor. This makes it easy to iteratively modify and test the applet while you compose it.

    Testing an applet from the Visual Composition Editor
    Do either of the following to test the applet:
    • Select the Run button on the tool bar.
    • From the Bean pull-down menu, select Run, then either In Applet Viewer or Run main.

    VisualAge performs the following steps:

    1. Saves the edit description of the bean
    2. Generates source code
    3. Compiles the class
    4. Runs the applet

  4. Export the applet class and related resources from VisualAge.

    Exporting an applet class
    Select the applet class in the Workbench window. Then, select Export from the File menu to open the SmartGuide - Export window. See the related task topic on Exporting a class for details.

  5. Edit your web page source file to add the applet. Specify the applet at the location in your page markup where you want the applet to run. If you had VisualAge generate an HTML file when you exported the applet, you need to edit the source to specify attributes for the <applet> tag.

    Adding an applet in an HTML file
    Use the <applet> tag to identify the applet class and to specify the dimensions of the bounding rectangle in which the applet is to run. The following example includes an applet named MyApplet. The applet is run in a rectangle that is 100 pixels wide and 80 pixels high.
    <applet code="MyApplet.class" width=100 height=80></applet>
    

    See books documenting HTML for other attributes that can be specified on the Applet tag.

For examples, see the BookmarkList class in the com.ibm.ivj.examples.vc.swing.bookmarklist and com.ibm.ivj.examples.vc.bookmarklist packages. These examples are shipped in the IBM Java Examples project.


Related procedures
Working with Beans Visually
Using VisualAge Beans in Visual Composition
Creating an applet
Creating a class
Exporting a class
Adding the IBM Java Examples project

Related references
Applet Beans
BookmarkList Sample