Adding state checking to your applet

Now that you have versioned an edition of your applet, you are ready to add state checking.

Desired behavior of the Remove button

Currently, the Remove button is always enabled, even if no items are in the list. Here is how the Remove button should work:

Overview of adding the desired behavior to the Remove button

To get the desired behavior for the Remove button, you need to:

Open your To-Do List applet in the Visual Composition Editor

First, open your To-Do List applet class in the Visual Composition Editor:

  1. Select the class for your To-Do List applet in the Workbench.
  2. From the Selected menu, select Open To and then Visual Composition.
  3. The free-form surface appears. It should look like this:

    tconnct3.GIF (5004 bytes)

Set the properties of the Remove button

Now set the properties of the Remove button so it is disabled when the applet starts:

    1. Select the Remove button and click mouse button 2. Select Properties from the pop-up menu that appears. The Properties secondary window appears.

      tpropdia.gif (4047 bytes)

    2. Select the field to the right of enabled. Select False from the drop down list in this field and close the Properties window. The Remove button should now appear disabled:

      trembdis.gif (204 bytes)

Create a new method to check if an item is selected

Next, create a new method in your To-Do List applet that checks to see if any items are selected in the To-Do List. To create a new method:

  1. Select the Methods page.
  2. Select Create Method or Constructor from the tool bar.
    tmeticon.gif (1035 bytes)

    The Create Method SmartGuide appears.

  3. In the Create Method SmartGuide, enter the following field beside Types:
  4. boolean enableRemove(

    Click Types. In the Types pop-up window, enter JL as the pattern.   Select JList from the Type Names panel. Click Insert.

  5. Add checkList as the parameter name for the JList type; then add a closing parenthesis. Your method should now be:
  6. boolean enableRemove(com.sun.java.swing.JList checkList)

    This specifies a method that takes one parameter (a JList) and returns a boolean value.

  7. Select Finish to generate the method.
  8. Select the new enableRemove(com.sun.java.swing.JList) method from the Methods list and add the code to implement it. If you are viewing this document in a browser, you can select the following code, copy it, and paste it into the Source pane. The finished method should look like this:
    public boolean enableRemove(com.sun.java.swing.JList checkList) {
            if (checkList.getSelectedIndex() < 0)
                    return false;
            else
                    return true;
    }
  9. To save this new method, click mouse button 2 in the Source pane and select Save from the pop-up menu that appears.

This simple method calls the getSelectedIndex method for its checkList parameter. If getSelectedIndex returns -1, there are no items selected in the list and enableRemove(com.sun.java.swing.JList) returns false (so the Remove button appears faded). Otherwise, enableRemove(com.sun.java.swing.JList) returns true (so the Remove button appears with solid black text like the Add button).

Add a connection to enable and disable the Remove button

Next, add the connection that enables the Remove button when an item is selected in the To-Do List:

  1. Select the Visual Composition page.
  2. Select the list and click mouse button 2. Select Connect then listSelectionEvents from the pop-up menu that appears. The mouse pointer changes to indicate that you are in the process of making a connection.
  3. Complete the connection by clicking mouse button 1 on the free-form surface and selecting Event to Code. Select method enableRemove() and select OK.
  4. The connection is incomplete because you need to provide information as to what JList should be checked for selected items. To complete the connection, select it, click mouse mouse button 2, and select Connect. Connect checkList to the JList1 this property.
  5. The boolean that is returned from the enableRemove method is used to set the enabled property of the  Remove button. Select the connection to the enableRemove method and click mouse button 2. Select Connect and select normalResult. Connect  normalResult to the Remove button enabled  property.

Every time an item is selected in the list, the enabled property of the Remove button is set to true.

tstacon3.gif (5739 bytes)

Saving and testing your changes

Before you continue, save your work and test it:

  1. To save the current state of your work in the Visual Composition Editor, select Save Bean from the Bean menu.
  2. To test the changes you made, select Run from the tool bar.
    trunicon.gif (1013 bytes)
  3. The Applet Viewer appears with your applet.
  4. Experiment with it to ensure that the behavior of the Remove button is correct. Ensure that the Remove button is disabled when the applet starts and then becomes enabled as soon as an item is selected in the To-Do List. Ensure that the Remove button becomes disabled again when nothing is selected in the To-Do List.

Congratulations! You have successfully added state checking to your To-Do List applet.

Now that you have a new level of your code working, create another versioned edition of it by following the steps in Versioning an edition of your applet.