Event Listener On The Appropriate Event Source example essay topic

979 words
What is an event? event / vent/ noun "a thing that happens, especially important"Every time the user types a character or pushes a mouse button, an event occurs. Any object can be notified of the event. All it has to do is implement the appropriate interface and be registered as an event listener on the appropriate event source". The above definition can be found at the java. sun. com web site, it is concise but not very clear in helping to understand in layman's terms what exactly is represented by an "event". A better analogy to the various parts of an event, as intended in Java with sources, listeners etc, could be represented, maybe, by a mother and her child at the park. As a parallelism to Java we can consider the child and mother as two "components".

The sandbox where the child is playing as a "panel" that contains the child and the bench where the mother is reading a as another "panel". And, finally, the park as the "frame" that contains these two "panels". As we can see we have basically all the components of a GUI. Now, in trying to create the analogy to events in event driven programming, we can consider the child as the object that can create an event (the "source" object) and the mother as another object that is "registered" to listen to events created by the "source".

If the child, for example, falls in the sandbox, it may start to cry. The crying of the child is an "event". And we can extend the analogy to Java by saying that the child "creates" a crying noise, just like in Java a source object creates an event object. Among the various people in the park the child's mother is "registered" to listen to the events created by her child.

And she will consequently, receive the event object (the crying noise of her child) and will "react" to it according to the necessities of the event. The actions taken by the mother represent the notion of event handling. She performs certain tasks determined by the nature of the event that she just "heard" form the source object. We can now leave this analogy and try to define in a more precise way, at least from a Java programming language point of view, what happens when an event is generated. A source object creates an event object and this event object is passed to any other object that has been registered to listen to the event generated by the source object. Once the "listening" object has received the event object, it will react to it by executing the appropriate methods defined in its "event handler".

Therefore an event handler in Java can be defined as a class that defines the way in which to respond to particular events. EVENT HANDLERS Event handlers are the classes that define how a "listening" object will respond to particular events generated by the "Source" objects the listener object is registered to. There are many different kinds of components in Java and each of these can create different kinds of events. The listening objects will therefore have to implement different classes of event handlers based on the different events that can be generated by the object that they are listening to. A few examples of events and the listener classes that are used with those events are: Event Listener type User clicks a button, ActionListener User closes a frame (main window) Window Listener User presses a mouse button Mouse Listener User moves the mouse over a component MouseMotionListener Component becomes visible Component Listener Component gets the keyboard focus Focus Listener Table or list selection changes ListSelectionListener There are therefore many different classes of eventListener classes that we can use to implement our event handlers, each with different methods to implement the reaction to the specific events. But, even if they implement different eventListener classes, all event handlers must have, in some form, the following three required bits of code: 1) In the declaration for the event handler class, code that specifies that the class either implements a listener interface or extends a class that implements a listener interface.

2) Code that registers an instance of the event handler class as a listener upon one or more components. 3) Code that implements the methods in the listener interface. In the following example we can easily identify the three required parts of code for event handling (in red) import javax. swing. JApplet; import javax. swing. JButton; import java. awt. Toolkit; import java. awt.

BorderLayout; import java. awt. event. ActionListener; import java. awt. event. ActionEvent; public class Beeper extends JApplet implements ActionListener { requirement 1 JButton button; public void init { button = new JButton ("Click Me"); getContentPane. add (button, BorderLayout. CENTER); button. addActionListener (this); requirement 2 } public void actionPerformed (ActionEvent e) { requirement 3 Toolkit. getDefaultToolkit. beep ; } } Taking a closer look at the example code above we can explain the various parts in the following way. To detect when the user clicks the on-screen button, the program has have an object that implements the ActionListener interface. The program registers as an action listener on the button (the event source), using the addActionListener method.

When the user clicks the on-screen button, the button fires an action event. This results in the invocation of the action listener's actionPerformed method. The single argument to the method is an ActionEvent object that gives information about the event and its source. It is important to know also that ActionPerformed is the ONLY method defined in ActionListener and it must be implemented.