Now that you have versioned an edition of your applet, you
are ready to add state checking.
Currently, the Remove button is always enabled, even if no
items are in the list. Here is how the Remove button should
work:
- When the applet starts, the Remove button should be
disabled.
- When an item is selected in the To-Do List, the Remove button
should be enabled.
- When a selected item in the To-Do List has been deleted, the
Remove button should be disabled.
To get the desired behavior for the Remove button, you need
to:
- Open the To-Do List applet in the Visual Composition Editor
- Set the properties of the Remove button so it is disabled when
the applet first starts
- Add an event-to-code connection to enable the button when an item is
selected in the To-Do List
First, open your To-Do List applet class in the Visual Composition
Editor:
- Select the class for your To-Do List applet in the Workbench.
- From the Selected menu, select Open To and then
Visual Composition.
- The free-form surface appears. It should look like this:

Now set the properties of the Remove button so it is
disabled when the applet starts:
- Select RemoveButton and click mouse button 2. Select
Properties from the pop-up menu that appears. The Properties
window appears.

- 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:

Next, add the connection that enables the Remove button when
an item is selected in the To-Do List:
- From the Visual Composition page, select the JList bean and click mouse
button 2.
- From the pop-up menu that appears, select Connect and then
Connectable Features. A connection window opens.
- From the Event list, select
listSelectionEvents. The mouse pointer changes to indicate
that you are in the process of making a connection.
- 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().
- 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.
- Select OK to close the window. Your To-Do List should
now look like this:

Before you continue, save your work and test it:
- To save the current state of your work in the Visual Composition Editor,
select Save Bean from the Bean menu.
- To test the changes you made, select
Run from the tool bar.
- The Applet Viewer appears with your applet.
- 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.
