Listening to events

 

Introduction

We are now going to work with the "PatternEventListener" package, which will allow us to listen to events generated by one of its attributes.

 

The "PatternEventListener" package contains a "TestApplet" class which generalizes the JDK's Applet class and which contains a "Button" type attribute (which is a JDK class), to which we will apply the pattern, as well as a text field that will be used to visualize the events we will listen to.

 

 

Initial model

 

Figure 47. Initial model

 

 

Calling the pattern

We are going to run the pattern on the "button" attribute of the "TestApplet" class (Figure 48).

 

Figure 48. Running the pattern

 

 

Selecting events

When the dialog box opens, carry out the following steps:

1.      Select the "focusGained" event in the list of available events.

2.      Click on the "Add" button.

3.      Select the "focusLost" event in the list of available events.

4.      Click on the "Add" button.

5.      Select the "keyPressed" event in the list of available events.

6.      Click on the "Add" button.

7.      Select the "keyReleased" event in the list of available events.

8.      Click on the "Add" button.

9.      Click on the "OK" button.

 

Figure 49. Dialog box for selecting events to listen to

 

 

Model after transformation

 

Figure 50. Transformed model

 

 

Summary of the elements created during the transformation

Application of the pattern created:

·         an inner class, "ButtonKeyListener", which generalizes the JDK "KeyAdapter" class, to react to "keyPressed" and "keyReleased" events

·         an inner class, "ButtonFocusListener", which generalizes the JDK "FocusAdapter" class, to react to the "focusGained" and "focusLost"" events

 

 

Entering methods of reaction to events

The bodies of the methods reacting to events remain to be entered. We will simply display here the traces in the applet's text field.

 

"focusGained" method of the "ButtonFocusListener" class:

getText().setText ( "The button has the focus" );

 

 

"focusLost" method of the "ButtonFocusListener" class:

getText().setText ( "The button has lost the

 focus" );

 

 

"keyPressed" method of the "ButtonKeyListener" class:

getText().setText ( "A key has been pressed" );

 

 

"keyReleased" method of the "ButtonKeyListener" class:

getText().setText ( "The key has been released" );

 

 

Generating code

You now have to generate and compile. Follow the steps below:

1.      Generate the code of the "PatternEventListener" package's classes.

 

Note:      If the "Automatically generate" parameter is active, it is not necessary to carry out these steps.  For more information on this parameter, please see "Objecteering Java Developer parameters".

 

 

Execution

We are now able to launch the applet.  To do this, simply click on the ‘Run the applet’ button of the Java tab for the "TestApplet" class.

 

Figure 51. Executing the applet

 

 

The applet is now activated.

 

Figure 52. Result of applet execution

 

1.      Click on the "Test" button.  The text field now displays "The button has the focus".

2.      Click on the text field.  The text field now displays "The button has lost the focus".

3.      Click on the "Test" button.  The text field now displays "The button has the focus".

4.      Press a key and hold it down.  The text field now displays "A key has been pressed".

5.      Release the key.  The text field now displays "The key has been released".