views:

656

answers:

5

I'm looking for a good tutorial into Java events. Something in the core framework that allows simple event semantics (similar to C#'s events).

Googling for Java events gives a page from Swing UI documentation and some other page from 2002, which hardly seems appropriate.

A: 

For something generic and not tied to GUI events you'll need to use PropertyChangeSupport

Nate
that's one specific type of event. you can create custom events yourself.
Scott Stanchfield
A: 

Nothing changed from that time. The only change is that now you can create anonymous classes to implement "handlers".

Differences from c#.

  1. There are no delegates, events, lambdas and so on;
  2. There now usefull basic classes. So everyone create its own BlaBlaListener, IMHO best one is is Listener with single method (handle?) with single argument (based on EventObject) so one can build EventLists and use generic handling and firing;
  3. Word Listener is something that in C# called Handler, but can have more then one "handler", means that this is simple inteface, so it can have more then one method;
  4. As for c# EventArgs there is EventObject. Other thing that in java noting usefull that depends on EventObject, so if you want you can safely ignore it;
Mike Chaliy
A: 

Well it depends on what kind of events you are looking for.

Are you looking for something like EMF event listeners...are you looking for UI Documentation like SWT or Swing...are you looking for everything?

http://www.java2s.com/Code/JavaAPI/org.eclipse.swt.events/Catalogorg.eclipse.swt.events.htm

PSU_Kardi
I'm looking for the 'basic' event class - the synchronization mechanism. I don't know what's SWT/EMF, and barely recognize Swing as a UI framework.
ripper234
A: 

I think the problem is that there is very little to an event.

Edit (after comments) Since it involves so little code, without the ability to have multiple inheritance, mixins or some sort of magic, it's not really possible to have the language help you much with this.

To do it manually, If you are creating a class that can issue events it's nothing tricky. You have to be able to add and remove a listener so you need two methods that take the listener interface as a parameter--just store them in a collection. When you want to notify each listener, just iterate over the collection and call the method on the event interface.

Bill K
The last paragraph of your question implies there is no built-in functionality equivalent to C#'s events (which encompasses exactly that - adding / removing callbacks, calling all callbacks, ...)
ripper234
Yes, there is no built-in support for events. Would be nice, but personally I prefer less magic/more explicit for the most part--but I think I'm alone in that.
Bill K
A: 

c# includes events as a language feature, and in c# "event" is a keyword. Java does not include events as a language feature, but many frameworks (especially GUI related ones), do use the event pattern.

Peter Recore