![]() |
![]() |
![]() |
![]() |
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 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' (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("");Now you can test the code that you have just created. Type a few words into the text area in your program. 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.We will now create the code to load a text file.
- Press the Load button to show its properties in the Composer. Choose its 'Listeners' page and select 'Listen for action events'. Select the Action page.
- Press the Code Sourcerer button from the Load button's Action page. Choose 'File operations...'. Press Next.
- Choose the second item, "Create a new File object from a FileDialog...". Press Next.
- The Code Sourcerer asks you to enter a title for the FileDialog. Enter 'Choose a file to load' for the title.
- The Code Sourcerer asks you to enter a name for the File object which will point to the file that the user chooses. Leave the default value of 'theFile' for the destination name.
- Select 'Load' mode.
- Press Done. Several lines of code should have appeared in the Action page which will launch a File Dialog and ask the user to choose a file to load. A file object has been created called theFile.
- Press the Code Sourcerer button again. Choose 'File operations...'. Press Next.
- Choose 'Read a String from a text file...'. Press Next.
- The Code Sourcerer asks for the name of a File object to read from. Leave the default value of 'theFile'. The Code Sourcerer also asks for the name of the string in which to store the data. Leave the default value of 'theText'.
- Press Done. Now you have created a File object, and read the text from the file that the user chose into the String, 'theText'.
- We will use the Code Sourcerer once more to put the text that we just read into the text area. Press the Code Sourcerer button and choose 'Change a property of an existing part...'. Press Next.
- Choose the part, 'text' to change. Press Next.
- Choose 'Change text of Text Area'. Press Next.
- Choose 'The following variable expression'. Remove any text in the text field to the right and replace it with theText. (Note: It is case sensitive!) Press Done.
You should see the following code in the Action page of the Load button.
You can now test this code by pressing the Load button. A FileDialog will appear. Choose any text file on your computer to load. The contents of that file should appear in the text area.
We will now create the code to save a text file.
- Press the Save button to show its properties in the Composer. Choose its 'Listeners' page and select 'Listen for action events'. Select the Action page.
- Press the Code Sourcerer button from the Save button's Action page. Choose 'File operations...'. Press Next.
- Choose the second item, "Create a new File object from a FileDialog...". Press Next.
- Enter 'Choose a filename to save' for the title.
Leave the default value, 'theFile', for the destination name.
Select 'Save' mode.- Press Done. Several lines of code should have appeared in the Action page which will launch a File Dialog and ask the user to choose a filename. A file object has been created called theFile.
- Press the Code Sourcerer button again. Choose 'File operations...'. Press Next.
- Choose 'Write a String to a text file...'. Press Next.
- The Code Sourcerer asks for the name of a File object to write to. Leave the default value, 'theFile', which is the name of the File object you just created. Press Next.
- The Code Sourcerer now asks where the text to be saved should come from. Select 'Another Part' and choose 'text' from the Choice box. This instructs the Code Sourcerer to save the current text from the component named 'text' (our TextArea).
- Press Done. Now you have created a File object, and saved the text to it.
You can test this code by typing a few words into the text area and pressing the Save button. A FileDialog will ask you for the filename to save it to. You can check that this worked by pressing the New button, and then using the Load button to read the text back.
Let's add one last thing to this program... A way to close it.
- Toward the top of the Composer window, find the part list Choice box and choose the first item, 'TextEditor'. This shows the settings for the window that our program is sitting in.
- Select the 'Listeners' page.
- We want to respond to the user trying to close the window. Select 'Listen for window events'. A new page appears in the properties notebook called 'Window'. Select this new page.
- There are seven kinds of window events. We want to respond to a 'Window Closing' event. From the Choice box to the left of the Code Sourcerer button, select the third item, labelled 'windowClosing'. (Be sure to select windowClosing and not windowClosed.)
- Press the Code Sourcerer Button.
- Choose 'Change a property of an existing part...'. Press Next.
- Select the first item, labelled 'TextEditor'. Press Next.
- Choose 'Dispose of this Frame'. Press Done.
The following code should have appeared in the Window Page for the windowClosing event.
You can test this code by closing the program window in the default manner for your operating system. (This is often done by double clicking the upper-left icon or by pressing a close button on the upper-right). You can get the window back by choosing Initialize Class from the Program menu in the Composer.
You have now finished designing your text editor. Save your work by choosing Exit from the File menu in the Composer. Choose Yes when asked if you want to save your changes.
Data Representations, Inc. http://www.datarepresentations.com support@datarepresentations.com sales@datarepresentations.com |
![]() |
![]() |
![]() |
![]() |