Tech I Enjoy Logo

Custom Search




  Home >> Java


Event handling and Java Technology
How can I extend Java event handling code to suite application 
specific event handling?

There are four parts to event handling, one is listener, second 
one is event object, the third one is the event container and 
fourth one is the event propagator.

Extension point for first two parts are provided by
Java api as java.util.EventListener and java.util.EventObject 
respectively. The third one (Event listener container) and
fourth one (event generator) has to be provided by programmer

Event generator should extend  event listener container,
and every event generator/source should add event listeners
added to it, and calls event methods and passes event object
associated with this event listener.

Let us implement a sample demo exercise: A demo listener implemented as follows: The first one is the event listener as follows: ----------------------------------------------------------------- public interface DemoListener extends java.util.EventListener { public void demoEvent(DemoEvent dm); } ----------------------------------------------------------------- and second one the event object as follows: ----------------------------------------------------------------- public class DemoEvent extends java.util.EventObject { Object obj; public DemoEvent(Object source) { super(source); obj = source; } public Object getSource() { return obj; } ...... } ----------------------------------------------------------------- The third part is the event container: ----------------------------------------------------------------- public class EventContainer { private Vector repository = new Vector(); DemoListener dl; public EventContainer() { } public void addDemoListener(DemoListener dl) { repository.addElement(dl); } public void notifyDemoEvent() { Enumeration enum = repository.elements(); while(enum.hasMoreElements()) { dl = (DemoListener)enum.nextElement(); dl.demoEvent(new DemoEvent(this)); } } ...... } ----------------------------------------------------------------- The fourth one is the event listener implementor, that implements event listener and implements event handling code/method. EventContainer adds implemented DemoListener and based on certain event, notifyDemoEvent methods executes demoEvent method for all the demo listeners in the repository inside EventContainer. If you like to share your comment/suggestions/feedback relating to this Page, you can do so by droping us an email at usingframeworks @ gmail . com with the subject line mentioning URL for this Page (i.e, /event-handling-java-code.php) or use this LINK. As per this website's privacy policy, we never disclose your email id, though we shall post your comments/suggestions/feedback with your name (optional) and date on this Page. If you don't want your comments/suggestions/feedback to be shared in this Page, please mention so in your email to us. Thank you very much..... If anything missed out , please let me know at techienjoy at yahoo . com
List of Examples on Java Platform :
List of Examples on Java Platform.
Interview Questions and answer of Java Technology :
Interview Questions and answer of Java Technology.
Thread design scenario using Java :
Thread design scenario using Java Technology.
Example ThreadPoolExecutor using Java :
ThreadPoolExecutor Example using Java Technology.
Thread Deadlock using Java :
Example reproducing a possible Thread 
using Java Technology.
Synchronized Block wait and notify With Example :
Example using wait and notify from within
synchronized block with code and explained
Java Reflection using Comparator :
Comparator using Reflection on Java Platform
and example discussed.
Example of Drag and Drop using Java Technology :
Example of Drag and Drop using Java Technology.
RMI and Java Stub and Skeleton :
Example using RMI using Java Technology.
Event Handling using Java Technology :
Example on how to use Event
and handling code using Java Technology.
Image creation using Java AWT :
In-Memory Image creation using AWT on Java Platform.
Thread wait and notify With Example :
Example using wait and notify within Thread 
with code and explained


References :
Tags: java comparator reflection
Tags: java example drag n drop
Tags: Java Interview Questions
Tags: java rmi tutorial stub skeleton
Tags: Java Thread Deadlock
Tags: Java Thread Design Scenarios
Tags: Java threadpoolexecutor
Tags: Java



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions