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:

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

Opening 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:
    Free-form surface, showing completed basic ToDoList

Setting the properties of the Remove button

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

  1. Select RemoveButton and click mouse button 2. Select Properties from the pop-up menu that appears. The Properties window appears.
    Property sheet for RemoveButton bean
  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:
    Disabled Remove button

Adding 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. From the Visual Composition page, select the JList bean and click mouse button 2.
  2. From the pop-up menu that appears, select Connect and then Connectable Features. A connection window opens.
  3. From the Event list, select listSelectionEvents. The mouse pointer changes to indicate that you are in the process of making a connection.
  4. Complete the connection by clicking mouse button 1 on the free-form surface and selecting Event to Code.

    The Event-to-Code Connection window appears. By default, VisualAge for Java creates a new method stub called jList1_ListSelectionEvents().

  5. Copy the following code over the method stub:
    public void jList1_ListSelectionEvents() {
            if (getJList1().getSelectedIndex() < 0)
                    getRemoveButton().setEnabled(false);
            else
                    getRemoveButton().setEnabled(true);
            return;
    }
    

    This method calls the getSelectedIndex() method of JList1. If the method returns -1, no items are selected in the list, and the enabled property of RemoveButton is set to false. Otherwise, enabled is set to true.

  6. Select OK to close the window. Your To-Do List should now look like this:
    Free-form surface, showing finished ToDoList applet with enhancements

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 tool Run from the tool bar.
  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.