Step 6: Create a Custom Control

In this step, you will create a new custom control. You will also insert a method into the control that calls a method on one of the controls you imported earlier.

  1. In the Project Explorer view, right-click the ServicesWeb/src/controls folder.
  2. Click New > Custom Control.
  3. In the New Control dialog, in the Control name box, enter MailingListControl.java, then click Finish.

    Through the preceding steps, you should have created the following two Java files in the controls folder:

  4. These files contain only a default framework at this point. You will add a method in the following steps.

  5. In the Project Explorer view, double-click ServicesWeb/Java Resources/src/controls/MailingListControlImpl.java.
  6. In the source editor, right-click anywhere within the source code for MailingListControlImpl.java and click Insert > Control.
  7. In the Select Control dialog, select CustomerControl - controls, then click OK.
  8. Add the following import statement to support code you are about to add:
       import model.Customer;
  9. After the variable declaration for customerControl, add the following method:
    public Customer[] getLocalCustomers()
    {
        return customerControl.getCustomersByState("CA");
    }
  10. Although you have created a new method in this class, the corresponding abstract method definition does not yet exist in MailingListControl.java, the interface that this class implements.

    To correct this situation place the cursor anywhere in the name of the method (getLocalCustomers) and press Ctrl+1. Select Create in super type 'MailingListControl' and press Enter.

    MailingListControl.java opens in the editor with the new method definition in place.

  11. Press Ctrl+Shift+S to save all of the edited files.

The getLocalCustomers method on this control uses the imported controls to query a sample database for all customers in a given state. In this example, we have hard-coded the state to be California. The data returned from the database is returned to the calling method as an array of Customer objects.

Click one of the following arrows to navigate through the tutorial:


Still need help? Post a question on the Workshop newsgroup.