Responding to events
So far, you have created the GUI of your application. You can enter numbers into the TextFields, and you can press the button, but the button does not yet know what it is supposed to do. Your program is still only a shell. We will now write code which will run when the compute tip Command is fired.
Simplicity will dynamically execute the code that you write so that you can test your program as you design it.
- In the Parts tree, click the 'Code before' item in the tree beneath 'compute tip' to show the appropriate Property sheet to the right in the Composer window.
Any valid java code can be typed into a Code area. We will use the Code Sourcerer to write all of the code which is needed for the button. The Code Sourcerer will write Java code for you, based upon some simple choices.
- Press the Code Sourcerer button in the Property sheet. This should be directly underneath the contextual help.
- A dialog appears with a list of choices. We want to get the text in the 'amount' TextField. Choose the second item, labelled 'Ask a part about one of its properties...'. Press Next.
- The Code Sourcerer now asks you which part you would like to query. Choose 'amount' (the TextField labeled 'Bill Amount $' in your program). Press Next.
- The Code Sourcerer now presents you with a list of the TextField's properties that can be queried. Choose 'The text of the TextField' if it is not already selected. Press Done.
- The Code Sourcerer now asks you where to store the value from the TextField. Replace the default choice with 'String amt'. Press Ok.
The following code should have appeared in the code area.
- Next the Code Sourcerer will help you write code to ask the 'percentage' TextField for its contents. Press the Code Sourcerer button.
- Choose the second option, 'Ask a part about one of its properties...'. Press Next.
- Choose 'percentage' from the list of parts. Press Next.
- Choose 'The text of the TextField' if it is not already selected. Press Done.
- The Code Sourcerer now asks you where to store the value. Replace the default choice with 'String perc'. Press Ok.
The following new code should have appeared in the code area.
Next you will need to add the code to do the calculation. The Code Sourcerer will generate this code for you as well.
- Press the Code Sourcerer button.
- Choose the option 'Perform floating point computations...'. Press Next.
- You will now see a screen where you can enter a calculation which will be evaluated. You can enter it as straight text (for example, typing 2.5 * 2), but in this case you want to see the result of a variable expression. Choose the third option, "The following variable expression".
- You want to multiply the total amount of the bill by the percent tip. In the space to the right, type the following text:
amt+"*"+perc+"/100"
and press Done.- A last screen will appear asking where you wish to store the return value. The default value 'String result' is fine, so press Ok.
The following new code will appear on one line
Next, you will use the Code Sourcerer to make the button display this result in the tip TextBox.
- Press the Code Sourcerer button.
- Choose the first option if it is not already selected, 'Change a property of an existing part...'. Press Next.
- The Code Sourcerer now asks you which part you would like to change. Choose 'tip' (the StringItem which will display the amount of the tip). Press Next.
- You will see a list of the properties which you can set in the StringItem. Choose the first option, 'Change text of String Item...'. Press Next.
- The Code Sourcerer now asks where the text should come from. In this case, you want the text to come from the String which you already set up. Choose the third choice, 'The following variable expression'. Replace the text there with the following text:
result
and press Done.The following new code will appear
Now you can test the code that you have just created. Type a number into the 'Bill Amount $' field in your program. Type '15' into the 'Percentage' field in your program. Then press the compute tip button. The amount of the tip is computed.
This is a good time to take a look at the code you have created. Open the File menu of the MIDlet Composer and choose 'View code for selected MIDlet...'. A new "Code Viewer" window will open and you will be able to see all the Java code you have written. Close the Code Viewer window when you are ready to move on.
| Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |