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:
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.
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.
Use the Action dialog box to add code that transmits the number of calls into the group class:
group.addButtonCallback(msg, evt);
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:
For this example, you can use the default operations name, Op1 provided by Gui builder.
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.
Use the Action dialog box to add code that transmits the number of calls into the group class:
group.findButtonCallback(msg, evt);
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:
For this example, you can use the default operations name, Op1, provided by GUI builder.
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.
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: