Applet Wizard

Visual J Plus Plus v6 provides an applet wizard named Applet On HTML that simplifies the job of creating applets. Rather than go through the steps of creating an empty project and adding the applet and HTML files manually, the Applet On HTML wizard builds a complete applet framework from which you can begin coding.

An Example Applet On HTML Applet

To demonstrate the strengths and weaknesses of the Applet On HTML wizard compared with the purely manual approach to creating applets we've been using so far in this chapter, let's create AWTApplet3. This applet transfers data from an input text field to an output text field. This problem is the same one we solved in the previous versions of AWTApplet earlier in the chapter.

Creating the project

From the File menu, choose New Project. From the New Project window, select Web Pages under the Visual J Plus Plus Projects folder. In the right-hand pane, choose Applet On HTML; type in the project name, AWTApplet3; and choose Open. This process creates a project with two members: Applet1.java and Page1.htm.

design work

The design work for this applet consists of the following steps:

  1. Open the HTML editor by double-clicking the Page1.htm file in Project Explorer.
  2. Select the Design tab.
  3. Open the Properties window.
  4. Select the applet in the Design window. (The applet initially isn't visible. Move the mouse pointer around within the Design window until the mouse pointer turns into a "move" icon, and then click the mouse button.)
  5. Resize the applet window to be about two-thirds of its original size.

If you examine the Properties window, you'll notice numerous applet properties that you can modify.

code

The Applet1.java code appears as follows. (I've left out sections of repetitive wizard-generated code.)

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
 * This class reads PARAM tags from its HTML host page and sets
 * the color and label properties of the applet. Program execution
 * begins with the init() method. */
public class Applet1 extends Applet
{
 /**
 * The entry point for the applet. */
 public void init()
 {
 initForm();
 usePageParams();
 // create the user panel and add it in the // middle of the applet window
 this.add("West", new Label(" "));
 this.add("East", new Label(" "));
 this.add("South", new Label(" "));
 this.add("Center", userPanel());
 }
 private final String labelParam = "label";
 private final String backgroundParam = "background";
 private final String foregroundParam = "foreground";
 /**
 * Reads parameters from the applet's HTML host and sets applet
 * properties.
 */
 private void usePageParams()
 {
 final String defaultLabel = "Default label";
 final String defaultBackground = "C0C0C0";
 final String defaultForeground = "000000";
 String labelValue;
 String backgroundValue;
 String foregroundValue;
 /** * Read the <PARAM NAME="label" VALUE="some string">,
 * <PARAM NAME="background" VALUE="rrggbb">,
 * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from
 * the applet's HTML host.
 */
 labelValue = getParameter(labelParam);
 backgroundValue = getParameter(backgroundParam);
 foregroundValue = getParameter(foregroundParam);
 if ((labelValue == null) || (backgroundValue == null) ||
 (foregroundValue == null))
 {
 /**
 * There was something wrong with the HTML host tags.
 * Generate default values.
 */
 labelValue = defaultLabel;
 backgroundValue = defaultBackground;
 foregroundValue = defaultForeground;
 }
 /**
 * Set the applet's string label, background color, and
 * foreground colors.
 */
 label1.setText(labelValue);
 label1.setBackground(stringToColor(backgroundValue));
 label1.setForeground(stringToColor(foregroundValue));
 this.setBackground(stringToColor(backgroundValue));
 this.setForeground(stringToColor(foregroundValue));
 }
 /**
 * Converts a string formatted as "rrggbb" to an awt.Color object
 */
 private Color stringToColor(String paramValue)
 {
 // …created by wizard…
 }
 .
 .
 .
 Label label1 = new Label();
 /**
 * Initializes values for the applet and its components
 */
 void initForm()
 {
 this.setBackground(Color.lightGray);
 this.setForeground(Color.black);
 label1.setText("label1");
 this.setLayout(new BorderLayout());
 this.add("North",label1);
 }
 //-------locally defined code-----------
 TextField sourceTF;
 TextField targetTF;
 Button button;
 /**
 * Create the user panel to go below the applet label.
 */
 Panel userPanel()
 {
 // create a vertical layout with the source text field,
 // target text field, and button
 Panel panel = new Panel();
 panel.setLayout(new GridLayout(0, 1, 50, 10));
 panel.add(sourceTF = new TextField());
 panel.add(targetTF = new TextField());
 // put the button in the middle of a horizontal layout
 Panel subPanel = new Panel();
 subPanel.setLayout(new GridLayout(1, 0));
 subPanel.add(new Label(" "));
 subPanel.add(button = new Button("Transfer"));
 subPanel.add(new Label(" "));
 panel.add(subPanel);
 // add an action listener to the button to
 // perform the transfer
 button.addActionListener(new TransferActionListener());
 return panel;
 }
 /**
 * Implement a transfer listener class that transfers
 * text from the source to the target text fields.
 */
 class TransferActionListener implements ActionListener
 {
 public void actionPerformed(ActionEvent ae)
 {
 targetTF.setText(sourceTF.getText());
 }
 }
}


The Applet On HTML wizard generated most of the code prior to the comment "locally defined code", except for the import of the java.awt.event package. The applet needs this package to gain access to the ActionListener class that is used to perform the text transfer operation.

The init() method begins with the wizard-generated calls to the initForm() and usePageParams() methods. I added the series of calls to add() at the bottom of the init() method.

The initForm() code performs the function of the similarly named method the Forms Designer creates for apps in that it sets up some of the basic applet properties. In Visual J Plus Plus v6, however, the applet initForm() has limited capability.

The usePageParams() method reads and implements the parameters defined in the HTML file. These parameters reflect the applet properties set in the Properties window.

The majority of the added code appears at the bottom of the Applet1 file, after the comment "locally defined code." This code is similar to that defined in AWTApplet2, using a vertical grid layout for the source text field, target text field, and button subpanel. The button subpanel uses a horizontal grid layout.

The TransferActionListener() method is invoked when the user chooses the Transfer button. This method reads the contents of the source text field and writes it into the target text field.

result

The output from executing AWTApplet3 is shown in Figure 14-9. (I edited the wizard-generated HTML file to change the title text.)

Screenshot

Screenshot-9. The appearance of the AWTApplet3 applet after transferring a line of text.

In my opinion, the Applet On HTML wizard doesn't save that much effort. In earlier chapters, we used the Forms Designer to lay out the appearance of Visual J Plus Plus v6 apps. The code it generated was both concise and powerful. All that was left to the programmer was to write the code to make the program actually do something. The net result was similar in feeling but more powerful than the advanced design capability of Microsoft Visual Basic.

By comparison, the applet Properties window offers limited capability. The code the Applet On HTML wizard generates is both lengthy and of questionable value. In addition, many of the settings within the Properties window appear to have little effect on the selected applet.

The Applet On HTML wizard shows a lot of promise, but this tool appears to be in need of improvement in future versions. Comments