Java programming course: 17.4 Removing an existing animal


Removing an existing animal

When you built the form for AnimalPanel you included a Remove button. Before writing the code so that it removes the selected animal (after asking for confirmation) you will change the status of the button so that it is only enabled (i.e., available to be clicked) when an animal is actually selected in the list.

Open AnimalPanel in Design mode and click the Remove button to select it. In the Properties window locate the enabled property (you may need to scroll to find it):

Properties window

You will see that the check box for this attribute is checked, indicating that the button is enabled by default, so un-check this check box now. You will notice that the button becomes greyed out to indicate that it cannot be clicked.

Switch into Source mode and modify the valueChanged() method to enable the button whenever a zookeeper is selected, or disable it again when nothing is selected:

@Override
public void valueChanged(TreeSelectionEvent e) {
    // A node has been selected from AnimalTree
    DefaultMutableTreeNode selectedNode = 
            (DefaultMutableTreeNode) animalTree.getLastSelectedPathComponent();
    removeButton.setEnabled(false);
    if (selectedNode != null) {
        Object selectedObject = selectedNode.getUserObject();
        if (selectedObject instanceof Animal) {
            Animal selectedAnimal = (Animal) selectedObject;
            animalEditor.setAnimal(selectedAnimal);
            removeButton.setEnabled(true);
        }
    }
}
 

Now switch back to Design mode and double-click the Remove button to generate its removeButtonActionPerformed() method. This method needs to ask the user to confirm that they really do want to remove the zookeeper and if so invoke the ZooAdministrator removeZooKeeper() method:

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultMutableTreeNode selectedNode =
        (DefaultMutableTreeNode) animalTree.getLastSelectedPathComponent();
    if (selectedNode != null) {
        Object selectedObject = selectedNode.getUserObject();
        if (selectedObject instanceof Animal) {
            Animal selectedAnimal = (Animal) selectedObject;
            int response = JOptionPane.showConfirmDialog(null,
                    "Do you really want to remove " +
                        selectedAnimal.getName() + "?",
                    "Confirm Remove", JOptionPane.YES_NO_OPTION);
            if (response == JOptionPane.YES_OPTION) {
                admin.removeAnimal(selectedAnimal);
                animalEditor.clearAnimal();
            }
        }
    }
}
 

After the animal is removed the editor is placed back into "add" mode by calling clearAnimal(). You should also now ensure the entry is removed from the tree display, so modify animalRemoved() of AnimalTree as follows:

@Override
public void animalRemoved(AnimalEvent event) {
    buildTree();
    model.reload();
    expandNodes();
}
 


Print
×
Stay Informed

When you subscribe, we will send you an e-mail whenever there are new updates on the site.

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Monday, 27 October 2025

Captcha Image