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:
- 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.
Overview of
adding the desired behavior to the Remove button
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.
- Create a new method that checks if any item is selected in the To-Do List.
- Add an Event-to-Code connection to enable the button when an item is selected in the To-Do
List.
Open your
To-Do List applet in the Visual Composition Editor
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:

Set the properties of the Remove
button
Now set the properties of the Remove button so it is disabled when the applet
starts:
- Select the Remove button and click mouse button 2. Select Properties from
the pop-up menu that appears. The Properties secondary 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:

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:
- Select the Methods page.
- Select Create Method or Constructor from the tool bar.
The Create Method SmartGuide appears.
- In the Create Method SmartGuide, enter the following field beside Types:
boolean enableRemove(
Click Types. In the Types pop-up window, enter JL as the pattern.
Select JList from the Type Names panel. Click Insert.
- Add checkList as the parameter name for the JList type; then add a closing parenthesis.
Your method should now be:
boolean enableRemove(com.sun.java.swing.JList checkList)
This specifies a method that takes one parameter (a JList) and returns a boolean value.
- Select Finish to generate the method.
- 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;
}
- 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:
- Select the Visual Composition page.
- 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.
- Complete the connection by clicking mouse button 1 on the free-form surface and
selecting Event to Code. Select method enableRemove() and select OK.
- 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.
- 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.

Saving and testing your changes
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.
