< Previous | Next >

Lesson 2.5: Enable the Delete button and confirmation dialog box

In this exercise, you will program the My Company Directory application to delete an employee record.

The following list describes the behavior that you want the application to use:
  • When you select an employee in the table, the Delete button is enabled.
  • When you click the Delete button, the Confirm Delete dialog box opens and asks you to confirm the deletion.
  • If you click the Yes button on the Confirm Delete dialog box, the employee record is deleted, the Confirm Delete dialog box closes, and the list of employees is refreshed.
  • If you click No, the deletion is canceled and the Confirm Delete dialog box closes.

Program the Delete button to be enabled or disabled based on whether a row is selected in the table

To program the Delete button to be enabled or disabled, add a listener to the table that enables the button when a row is selected.

  1. Select the employeesTable in the Java Beans view. The source view highlights the following line:
    employeesTable = new JTable();
  2. Immediately after this line, add a new ListSelectionListener and valueChanged event to the employeesTable:
    employeesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
       public void valueChanged(ListSelectionEvent e) {
         getDeleteButton().setEnabled(getEmployeesTable().getSelectedRowCount() != 0);
       }
    });
  3. After you add these lines of code, the source editor marks them as errors until you import ListSelectListener and ListSelectionEvent. To add the required imports, click Source > Organize Imports on the main menu. The following lines are added to the imports section of the class:
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
Now, when a row in the table is selected, the Delete button is enabled.

Program the Confirm Delete dialog box to open when Delete is clicked

Add an actionPerformed event to the Delete button, and program the event to open the Confirm Delete dialog box.

  1. Right-click the Delete button and select Events > actionPerformed. The following event stub is added to the getDeleteButton() method:
    deleteButton.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) {    
         System.out.println("actionPerformed()"); 
         // TODO Auto-generated Event stub actionPerformed()
       }
    });
  2. Replace this generated stub with the following code, which sets the Confirm Delete dialog box to be visible when the button is clicked:
    deleteButton.addActionListener(new java.awt.event.ActionListener() { 
       public void actionPerformed(java.awt.event.ActionEvent e) {    
         getConfirmDialog().setVisible(true);
       }
    });

Bind the text field in the Confirm Delete dialog box

Bind the text field in the Confirm Delete dialog box to display the first name of the employee to be deleted.

  1. On the Java Beans view or design area, select the employeeToDeleteField text field, and click the Bind tab.

    Confirm Delete dialog box before binding

  2. On the Field Data Bindings dialog box, select the selectedEmployeeRecord data object and the firstName field, then click OK.

    The text field is now bound to the firstName column of the selected row in the employeesTable.

    Confirm Delete dialog box after binding

  3. To make sure that this field is read-only, set the autoEditable property for the field's binder to false.

Bind the Yes button to perform the deletion

Bind the Yes button to call the removeEmployee(java.lang.Integer) method on the Web service.

  1. Select the Yes button, and click the Bind tab to open the Component Action Bindings dialog.
  2. In the Source type field, select Web Service.
  3. In the Data source field, select webServiceDataSource.
  4. From the Source service list, select removeEmployee(java.lang.Integer).
  5. The Name field automatically changes to removeEmployeeAction. Accept this default.
  6. In the Argument field, select selectedEmployeeRecord.
  7. In the Property field, select employeeID. Because the removeEmployee() method takes an integer as its argument, you use the employee ID of the selectedEmployeeRecord.
  8. Set the Initial state of the button to Enabled.
  9. For the Enablement rules, select Ignore for each of the conditions.

    This component state means that the Yes button will always be enabled, since there is no need for it to change its state.

    Component Action Bindings dialog box for the Yes button

  10. Click OK.

Add an event to hide the Confirm Delete dialog after the employee is deleted

In this step you add an event to the Yes button's binder (not the Yes button itself). You want the Confirm Delete dialog box to close after the employee is removed, which means after the binder has successfully called the service on the data source.

Add the following code to the getRemoveEmployeeAction() method:
removeEmployeeAction.addActionBinderListener(new jve.generated.IActionBinder.ActionBinderListener() { 
   public void afterActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) {    
     getConfirmDialog().setVisible(false);
   }
   public void beforeActionPerformed(jve.generated.IActionBinder.ActionBinderEvent e) {} 
});

This event code hides the Confirm Delete dialog box after the binder's action is performed.

Lesson checkpoint

Now, when you run the My Company Directory application you can select an employee in the table, click the Delete button, and click Yes to confirm the deletion. The employee record will be removed from the directory, and the list of employees will reflect the removal.

< Previous | Next >