Responding to events
So far, you have created only the shell of your application. You can type in the text area, and you can press the buttons, but the buttons do not yet know what they are supposed to do.
In Java, components respond to user interaction using Event Listeners. For example, a button can respond to being pressed by listening for Action Events.
Simplicity for Java lets you select the type of events that each component should listen for and specify the code that should be executed in response. Simplicity for Java will dynamically execute this code so that you can test your program as you design it.
Let's begin with the New button. We would like the New button to clear any text that is currently in the text area, so that new text may then be entered.
- Click the New button to show its properties in the Composer window.
- Select the third page from the properties notebook, labelled 'Listeners'. You should now see a list of the types of events that a button can listen for.
- Select 'Listen for Action events'. A new page appears in the properties notebook for the New button labelled 'Action'.
- Select this new 'Action' page. You should now see an empty Java Editor where you can specify the code that you wish to have executed whenever the New button is pressed.
To write the code, we will use the Code Sourcerer. The Code Sourcerer will write Java code for you, based upon some simple choices.
- Press the Code Sourcerer button toward the top of the 'Action' page for the New button. A dialog appears with a list of choices. We want to clear the text in the text area.
- If it is not already selected, choose the first item, labelled 'Change a property of an existing part...'. Press Next.
- The Code Sourcerer now asks you which part you would like to change. Choose 'text' from the list (the TextArea in your program). Press Next.
- The Code Sourcerer now presents you with a list of the text area's properties that can be changed. Choose 'Change text of Text Area...'. Press Next.
- The Code Sourcerer now asks you where to get the new text for the Text Area. We want to simply erase the current text, so choose the first option, and leave the text field to the right blank. Press Done.
The following code should have appeared in the Action Page.
text.setText("");Simplicity for Java contains special technology called Execution-On-The-Fly which enables you to test your code without haveing to leave the Composer or even invoke the Java compiler. This Execution-On-The-Fly technology is unique to Data Representations' products.
- Type a few words into the TextArea. Then press the New button. The text is cleared.
- Try changing the code in the New button's Action page to
text.setText("Hello World!");
and press the New button. Each change that you make is immediately integrated into the working model of your program. Test the change by pressing the New button again.- Remove the semicolon from the end of the line of code. Without this semicolon, the code cannot work properly.
- Press the New button. An error message will appear showing you exactly what the problem is.
- Correct the error by putting back the semicolon.
| Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |