Java programming course: 16.8 Removing an existing zookeeper


Removing an existing zookeeper

When you built the form for ZooKeeperPanel you included a Remove button. Before writing the code so that it removes the selected zookeeper (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 a zookeeper is actually selected in the list.

Open ZooKeeperPanel 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(ListSelectionEvent e) {
    // A zoo keeper has been selected from ZooKeeperList
    Object selected = zooKeeperList.getSelectedValue();
    if (selected != null) {
        ZooKeeper zooKeeper = (ZooKeeper) selected;
        zooKeeperEditor.setZooKeeper(zooKeeper);
        removeButton.setEnabled(true);
    } else {
        // Nothing selected
        removeButton.setEnabled(false);
    }
}
 

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 zoo keeper and if so invoke the ZooAdministrator removeZooKeeper() method:

private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {
    ZooKeeper zooKeeper = (ZooKeeper) zooKeeperList.getSelectedValue();
    int response = JOptionPane.showConfirmDialog(null,
            "Do you really want to remove " + zooKeeper.getName() + "?",
            "Confirm Remove", JOptionPane.YES_NO_OPTION);
    if (response == JOptionPane.YES_OPTION) {
        admin.removeZooKeeper(zooKeeper);
        zooKeeperEditor.clearZooKeeper();
    }
}
 

After the zookeeper is removed the editor is placed back into "add" mode by calling clearZooKeeper(). You should also now ensure nothing is selected in the list of zookeepers, so modify the zooKeeperRemovedEvent() of ZooKeeperList as follows:

@Override
public void zooKeeperRemoved(ZooKeeperEvent event) {
    model.loadModel();
    removeSelectionInterval(0, model.getSize());
}
 
  • The removeSelectionInterval() method de-selects all items in a list between the range of indexes specified in the arguments;
  • Also note that removeSelectionInterval() automatically triggers a ListSelectionEvent so you will find the Remove button becomes disabled.


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
Saturday, 13 December 2025

Captcha Image