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
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() );
}
It rocks!
Next Steps
- Download the code and check it out! (See Member Resources)
- Comment below with your thoughts.
Member Resources
You must be a 'Free Member' to see this content. With your access you will enjoy members-only content like this, HD video tutorials, and access to complete source code.
Ready To Join?
- Sign up for 'Free Member' level now. Its free!
- Sign up for 'Paid Member' level to enjoy much, much more! (Invite Only)
- Log in and enjoy!













