Previous Next vertical dots separating previous/next from contents/index/pdf

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 Package Explorer view, right-click the ServicesWeb/src/controls folder.
  2. Click New > Custom Control.
  3. The New Control dialog box appears.

    Enter MailingListControl.java in the Control name text box and click Finish.


  4. The preceding steps instructed Workshop to create two new Java files in the controls folder:
    • MailingListControl.java - the control interface file
    • MailingListControlImpl.java - the control implementation file that implements MailingListControl.java.

    These files contain only a default framework at this point. You will add a method in the following steps.
  5. in the Package Explorer view, double-click on 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 and click OK.
  8. Now you are ready to add the new method.

    After the variable declaration for customerControl, add the method:
      public Customer[] getLocalCustomers()
      {
      return customerControl.getCustomersByState("CA");
      }
  9. You will see an error because of the undefined type Customer. Add this line of code to the imports section of the class:
       import model.Customer;
  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 class that this class implements.

    To correct this situation place the editor's 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 abstract method definition in place.
  11. Press Ctrl+Shift+S to save your work.

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:

 

Skip navigation bar   Back to Top