Event handlers

If you order your custom term paper from our custom writing service you will receive a perfectly written assignment on Event handlers. What we need from you is to provide us with your detailed paper instructions for our experienced writers to follow all of your specific writing requirements. Specify your order details, state the exact number of pages required and our custom writing professionals will deliver the best quality Event handlers paper right on time.


Our staff of freelance writers includes over 120 experts proficient in Event handlers, therefore you can rest assured that your assignment will be handled by only top rated specialists. Order your Event handlers paper at


Our affordable prices!


Event Handlers


What is an event?


event /vent/ noun


"a thing that happens, especially important"


Essay help on Event handlers


"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) WindowListener


User presses a mouse button MouseListener


User moves the mouse over a component MouseMotionListener


Component becomes visible ComponentListener


Component gets the keyboard focus FocusListener


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.


) Code that registers an instance of the event handler class as a listener upon one or more components.


) Code that implements the methods in the listener interface.


In the following example we can easily identify the three required parts of code for eventhandling (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


}


public void actionPerformed(ActionEvent e) { ᠖ requirement


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 listeners 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.


Please note that this sample paper on Event handlers is for your review only. In order to eliminate any of the plagiarism issues, it is highly recommended that you do not use it for you own writing purposes. In case you experience difficulties with writing a well structured and accurately composed paper on Event handlers, we are here to assist you. Your cheap custom college paper on Event handlers will be written from scratch, so you do not have to worry about its originality. Order your authentic assignment and you will be amazed at how easy it is to complete a quality custom paper within the shortest time possible!


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.