permaculture playing cards
The moose likes Java in General and the fly likes Java Event handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java Event handling" Watch "Java Event handling" New topic

Java Event handling

Ranadhir Nag
Ranch Hand

Joined: Mar 09, 2006
Posts: 138
Can someone point me to a sample involving non-component based event handling?
All the samples that i find are event handlers being tied to AWT components?
I just wish to write a simple server which calls back on registered listeners(clients),by programmatically raising an event from one of the clients.
Any help will be appreciated.
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Have you taken a look to the java.util.Observer and java.util.Observable?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'm not a fan of Observer and Observable in the library. Before I even found them I made my own pub-sub with an intermediary. The flow is described HERE. I make an interface with methods for the events, implement it once in the intermediary and again in each subscriber. Here's an example with two events:

The abstract Publisher has (dead simple) methods to subscribe and unsubscribe. Some may be offended that I have to copy-paste that little loop for every event. But the flexibility I have here is pretty cool. I can pass parameters and get return types. I could let all the listeners vote on what to do next or let one of them signal that it has consumed the event and stop looping. I can have multiple publishers for the same event. Publishers can come and go while the subscription persists. Good fun.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Java Event handling
jQuery in Action, 3rd edition