Listeners Essay

Submitted By thinapay
Words: 5098
Pages: 21

1. Event Listener
An Event Listener, once set to an applet object waits for some action to be performed on it, be it mouse click, mouse hover, pressing of keys, click of button, etc. The class you are using (e.g. JButton, etc.) reports the activity to a class set by the class using it. That method then decides on how to react because of that action, usually with a series of if statements to determine which action it was performed on. source.getSource() will return the name of the object that the event was performed on, while the source is the object passed to the function when the action is performed. Every single time the action is performed, it calls the method. an event listener is used to process events. For example, a graphical component like aJButton or JTextField are known as event sources. This means they can generate events - when a user clicks on the JButton or types text into the JTextField. The event listeners job is to catch those events and do something with them.
Event listeners are actually different types of interfaces.

There are many types : theActionListener, ContainerListener, TextListener,WindowListener to name a few. Each interface defines one or more methods that must be implemented by a class in order for the event to be processed.

You can define a Listener interface: public interface EventListener { void fireEvent(Event e); }
Then in your code: EventListener lst = new EventListener() { @Override public void fireEvent(Event e) { //do what you want with e } } someObject.setListener(lst); someObject.somethingHappened(); Then in someObject (in practice you would probably hold a list of listeners): public class SomeObject() { private EventListener lst; public void setListener(EventListener lst) { this.lst = lst; } public void somethingHappened() { lst.fireEvent(new Event("Something Happened")); } }

listener
The object that receives a notification when an event of the specified type occurs. This must be an object implementing the EventListener interface, or simply a JavaScript function.

-------------------------------------------------
Example
-------------------------------------------------
Add a simple listener
HTML Content
-------------------------------------------------
<table id="outside">
-------------------------------------------------
<tr><td id="t1">one</td></tr>
-------------------------------------------------
<tr><td id="t2">two</td></tr>
-------------------------------------------------
</table>
CSS Content
-------------------------------------------------
#t1, #t2 { border: 2px solid gray; }
-------------------------------------------------
#t2 { background-color: pink; }
JavaScript Content
-------------------------------------------------
// Function to change the content of t2
-------------------------------------------------
function modifyText() {
-------------------------------------------------
var t2 = document.getElementById("t2");
-------------------------------------------------
if (t2.firstChild.nodeValue == "three") {
-------------------------------------------------
t2.firstChild.nodeValue = "two";
-------------------------------------------------
} else {
-------------------------------------------------