Handling the Add button
All that remains now is to write some code for the Add button to enable the user to switch from "change" mode back into "add" mode. Go to the Design view of ZooKeeperPanel and double-click the Add button to generate its addButtonActionPerformed() method. All it needs to do is invoke clearZooKeeper() on the zooKeeperEditor instance:
After adding a new zookeeper it would be best to ensure nothing is selected in the list, since the form remains in "add" mode. Update the zooKeeperCreated() method in ZooKeeperList:
You will have noticed that this is the same line of code that you previously added to zooKeeperRemoved(). Even though it is only a single line of code, given its slightly involved argument values there is a case for refactoring it into its own helper method. Define a new method in ZooKeeperList called deselectListItems():
Now you can modify the zooKeeperCreated() and zooKeeperRemoved() methods to invoke the new method:
Returning to the addButtonActionPerformed() method in ZooKeeperPanel, that should also cause any selected item in the list to be de-selected, and this is now possible since you have created the deselectListItems() method:
That completes the coding for the zookeeper panels, and you should now be able to add, change and remove as many zookeepers as you wish. Bear in mind, however, that as it stands the data is only stored for as long as the application remains open. Saving the data to a disk file is covered in Section 19.