Adding Behavior to the Card File

Now that you have the GUI laid out for your card file, you need to add behaviors (also called "operations" or "callbacks") to the components in the layout.

First, identify which components have operations. The Name, Phone, and Email text boxes do not require operations since users only type a string in each field. The Entries list only displays names and numbers, so it has no operation.

The Add, Find, Email, and Delete buttons do have operations. Start by adding an operation to the Add button that takes the strings entered in the Name and Phone text boxes and adds them to a list of items.

To define the operation for the Add button:

  1. Select the Add button in the layout window.
  2. Open the attributes editor by clicking on its button in the main tool bar.
  3. Click on the Edit operations button in the attributes editor.
  4. Click on the Insert button to add an operation.

    For this example, you can use the default operations name, Op1, provided by Gui builder. Ordinarily, you would give the operation an easily identifiable name.

  5. Click on the Actions button.

    Since the filter type is Event and the Event id is Action Event (the default type and ID), you can accept the default values. There is no need to set the Filter parameters.

  6. Select the action type, Execute Code.

    Use the Action dialog box to add code that transmits the number of calls into the group class:

    group.addButtonCallback(msg, evt);
    
  7. In the source editor, add code that defines the behavior for the button. Code is added to the group file, which should already be displayed in the editor. If it isn't, click the Open button in the Edit/Debug toolbar and select the project.java file for the card file project.

    You need to add code that combines the strings in the Name, Phone, and E-mail text boxes and retrieves the combined string. The string then needs to be added to the Entries list. The following code example shows one way to produce the desired action.

    public void addButtonCallback(Message msg, Event evt) {
    String[] listContents = (String []) gui.namesList.get("items");
    String newItem = (String) gui.nameTextfield.get("text") + " " +
                               (String) gui.phoneTextfield.get("text") + " " +
                               (String) gui.emailTextfield.get("text");
    int len = 0;
    if (listContents != null){
        len = listContents.length;
    }
    String[] newContents = new String[len + 1];
    int i = 0;
    
    for (i=0; i < len; i++) {
      newContents[i] = listContents[i];
    }
    newContents[len] = newItem;
    gui.namesList.set("items",  newContents);
    }
    

For information on what attributes can be set for a particular component, see Visual Java GUI Builder Runtime Packages.

To define the operation for the Find button:

  1. Select the Find button in the layout window.
  2. Open the attributes editor by clicking on its button in the main tool bar.
  3. Click on the Edit operations button in the attributes editor.
  4. Click on the Insert button to add an operation.

    For this example, you can use the default operations name, Op1 provided by Gui builder.

  5. Click on the Actions button.

    Since the filter type is Event and the Event id is Action Event (the default type and ID), you can accept the default values. There is no need to set the Filter parameters.

  6. Select the action type, Execute Code.

    Use the Action dialog box to add code that transmits the number of calls into the group class:

    group.findButtonCallback(msg, evt);
    
  7. In the source editor, add code that defines the behavior for the button. Code is added to the group file, which should already be displayed in the editor. If it isn't, click the Open button in the Edit/Debug toolbar and select the project.java file for the card file project.

    You need to add code that takes the string in the Name text box, locates its match in the Entries list, and displays it. The following code shows one way to produce this action.

    public void findButtonCallback(Message msg, Event evt){
        List listBody = (List) gui.namesList.getBody();
        int numItems = listBody.countItems();
        String nameToFind = (String) gui.nameTextfield.get("text");
        String currentName = "";
        int i = 0;
        int j = 0;
        int len = 0;
        int nameLen = nameToFind.length();
                              
        for (i=0; i < numItems; i++) {
             currentName = listBody.getItem(i);
             len = currentName.length();
             if (len >= nameLen && len != 0 && nameLen != 0) {
                 for (j=0; j < len - nameLen + 1; j++) {
    	          if (currentName.regionMatches(true, j, nameToFind,
                                                    0, nameLen) == true) {
                          listBody.select(i);
                          return;
                      }
                 }
             }
        }
    }
    

To define the operation for the Delete button:

  1. Select the Delete button in the layout window.
  2. Open the attributes editor by clicking on its button in the main tool bar.
  3. Click on the Edit operations button in the attributes editor.
  4. Click on the Insert button to add an operation.

    For this example, you can use the default operations name, Op1, provided by GUI builder.

  5. Click on the Actions button.

    Since the filter type is Event and the Event id is Action Event (the default type and ID), you can accept the default values. There is no need to set the Filter parameters.

  6. Select the action type, Execute Code.

    For the Delete button, you need to add code that takes the entry in the Name field and deletes it from the Entries list.

    import java.awt.List;
    List listBody = (List) gui.namesList.getBody();
    int selectedIndex = listBody.getSelectedIndex();
    
    listBody.delItem(selectedIndex);
    

When you have finished adding behavior to the buttons and saved the operations (click OK in the Operations dialog box), you need to save and generate the source file to include the newly added behavior:

To generate the source file:

To build the application:

After the build process completes, you can test the card file application by clicking on the Run button in the toolbar.

To attach another component created with GUI builder to the card file, see Adding a Mail Dialog Box.

See also:

Making Your Own Card File
Adding a Mail Dialog
Adding Behavior to Components