Event Dispatching in Java
My professional focus is on client-side development. For a recent project, I re-learned some substantial Java concepts and completed server-side coding.
During the project, I was surprised to find there is no native java event messaging (event dispatching and event listening). While some UI frameworks have a solution, the solution appears to be coupled to those frameworks.
So I created a custom Java solution to handle it. The system uses common Observer pattern and modeled it after the spirit of what is present in ActionScript 3.0. I used abstraction and interfaces so its fairly decoupled and flexible. Take a look.
Usage
[actionscript3]
public EventsDemo()
{
// CREATE
SampleEventDispatcher s = new SampleEventDispatcher();
// LISTEN
s.addEventListener(Event.EVENT_NAME, this, "_onEvent1");
//TEST ‘HAS’
/*
if (s.hasEventListener(Event.EVENT_NAME, this, "_onEvent1") ){
//TEST ‘REMOVE’
s.removeEventListener(Event.EVENT_NAME, this, "_onEvent1");
}
*/
// TEST A DISPATCH OF EVENT (IN A FAKE, TEMPORARY WAY)
s.doDispatchTestEvent(new Event (Event.EVENT_NAME, this));
}
public void _onEvent1 (Event aEvent)
{
System.out.println (" _onEvent1(): " + aEvent + " , " + aEvent.getType() + ", " + aEvent.getTarget() );
}
[/actionscript3]
It rocks!
Next Steps
- Download the code and check it out! (See Member Resources)
- Comment below with your thoughts.
Member Resources
[private_Free member]Enjoy this members-only content!
- Download my JavaDemos
- Create ‘EventDispatcherDemo’ project set compiler dependency to project ‘RMCCodeLibraryJava’ and publish ‘EventDispatcherDemo’.
[/private_Free member]