TOC PREV NEXT INDEX



Responding to events


So far, you have created only the shell 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.

On the Palm OS Platform, any part of the GUI can potentially respond to any pen up, pen down, pen move, or key down event which the Palm OS Platform receives. Simplicity lets you specify the Java source code to execute in response to these events.

Simplicity will dynamically execute the code that you write so that you can test your program as you design it.

There is only one part in the Tip Calculator which needs additional code written for it, and that is the 'compute tip' button.

  1. Click the 'compute tip' button to show its properties in the Composer window.
  2. Select the second page from the properties notebook, labelled 'penDown'. The code in this area will execute when the pen comes down on a button.

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.

  1. Press the Code Sourcerer button toward the top of the 'penDown' page for the 'compute tip' button.
  2. 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.
  3. 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.
  4. The Code Sourcerer now presents you with a list of the TextField's properties that can be queried. Choose 'TextField Text' if it is not already selected. Press Done.
  5. 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 Pen Down Page.

String amt = amount.getText();
  1. Next the Code Sourcerer will help you write code to ask the 'percentage' TextField for its contents. Press the Code Sourcerer button.
  2. Choose the second option, 'Ask a part about one of its properties...'. Press Next.
  3. Choose 'percentage' from the list of parts. Press Next.
  4. Choose 'TextField Text' if it is not already selected. Press Done.
  5. 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 Pen Down Page.

String perc = percentage.getText();

Next you will need to add the code to do the calculation. The Code Sourcerer will generate this code for you as well.

  1. Press the Code Sourcerer button.
  2. Choose the option "Perform floating point computations...". Press Next.
  3. 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".
  4. 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.
  5. 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

String result = new com.datarp.calc.Calculator (String.valueOf(amt+"*"+perc+"/100")).evaluate();

Next, you will use the Code Sourcerer to make the button display this result in the tip TextBox.

  1. Press the Code Sourcerer button.
  2. Choose the first option if it is not already selected, 'Change a property of an existing part'. Press Next.
  3. The Code Sourcerer now asks you which part you would like to change. Choose 'tip' (the TextBox which will display the amount of the tip). Press Next.
  4. You will see a list of the properties which you can set in the TextBox. Choose the first option, 'TextBox text...'. Press Next.
  5. 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

tip.setText(String.valueOf("$"+result));
paint();

Some kjava parts will paint themselves whenever they change (like the TextField). The TextBox, on the other hand, will not do so. The Code Sourcerer has added the code to make the whole screen repaint.

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.


Data Representations, Inc.
http://www.datarepresentations.com
support@datarepresentations.com
sales@datarepresentations.com
TOC PREV NEXT INDEX