Does Java have built-in support for events similar to those of C #?

I am a little confused by the fact that, as I heard, Java does not make events.

But I know that these are GUI events.

Am I missing something? Does java have an event handling mechanism?

I know that I can implement a publisher subscriber template, but I'm looking for built-in support in Java.

It seems that I remember something about Java Adding events to Java 5 or 6, but I can’t remember where I heard it, and I can do it.

I basically wrap the device in a java class, and the device fires events, and I'm looking for the most logical way to expose this. I come mostly from .Net Background, and I am looking for something like events in .Net (C #)

Any help would be appreciated.

+7
java events
Jan 12 '09 at 16:46
source share
9 answers

As you said, you can do the same with the publisher-subscriber / observer template. It just takes a bit more work.

And no, Java does not have built-in event support, such as C # with delegates.

+14
Jan 12 '09 at 16:49
source share

The Subscribe / Publish mechanism proposed by others will allow you to implement synchronous events. For asynchronous event loops (fire and forget) You might want to look at the "actors". Actor<A> consists of a type A message (event) handler, as well as a thread strategy for executing the handler. This allows parallel processing of an asynchronous event, for example:

 public class EventExample { public static void main(String[] args) { while ((char c = System.in.read()) >= 0) { eventHandler.act(c); } } public static Actor<Character> eventHandler = Actor.actor(Strategy.simpleThreadStrategy(), new Effect<Character>() { public void e(Character c) { // Do something with c here. } }); } 

Get the actors library here. See here Javadoc.

+4
Jan 12 '09 at 18:56
source share

From what I remember about Java (didn't work with it after> 6 months), I don't think there are ala.NET events. Your best bet is probably to use the publisher / subscriber pattern. It is pretty easy to implement and pretty reliable.

+1
Jan 12 '09 at 16:50
source share

Here is an article on this topic also see. What model / structure of the Actor library for Java?

+1
Jul 28 '10 at 21:34
source share

Swing or AWT, which are Java UI tools, both handle events. For an example, check out

0
Jan 12 '09 at 16:50
source share

I am not aware of C #, but Java Beans, which are not user interface components, can notify Listeners of state changes through events:

http://java.sun.com/developer/onlineTraining/Beans/beans02/

This has been part of the Java Beans specification since it first appeared. Pay attention to the date in the tutorial: this is vintage 2000.

The source and listener must work in the same JVM. You will need to use a proxy to communicate with something running in another JVM, but this can be done.

I Googled for a C # event tutorial and found this . Registering Java listeners and triggering events related to property changes resembled what I saw when I looked through the C # tutorial. Did I miss something important? (I admit that I did not read deeply - now there is no time.)

0
Jan 12 '09 at 17:15
source share

There is no built-in support in Java - you will need to execute the observer pattern (aka Subcribe / Publish). Its pretty straightforward - no more than a dozen lines of code.

0
Jan 12 '09 at 17:19
source share

You can also take a look at the open source EventBus library , which, unlike the standard publisher / subscriber template, uses an intermediary class (event bus) to reduce the hard link required in the publisher / subscriber template (where subscribers must know about all publishers to register on them, which is not always possible and often cumbersome).

I used it in GUI applications, but it can work with any events.

0
Jan 12 '09 at 23:08
source share

You need threads. The GUI in java has events, because there is a dedicated thread for sending user interfaces. There is also something like "Observer" and "Observable" (this is a taste-neutral version of the event listeners from the GUI material), but this is just a design template ...

-one
Jan 12 '09 at 16:50
source share



All Articles