2012-05-26

Elegant Event Handling in Java

Here's a comment I added to a great post with a very nice piece of Java code:
Elegant Event Handling in Java

Yes, that's very elegant. I was implementing some Java class, when I went "hey, I need to handle events". I know how to do this in Delphi and .NET/C#, and I know how to use Swing events (even if I don't really like their approach).

So I've looked into JButton searching for "actionPerformed()" method, but no luck.
Then I went to AbstractButton and found "fireActionPerformed()" - which looked pretty ugly.
I even went to EventListenerList - to confirm my suspicion, that there is a mess in Java regarding events. And it is inherited from old, pre-generics, Swing design.

I knew there must have been some better way.

I've found Mr HappyObject - funny, but not so useful.
Then this: How To Create Your Own Events In Java - little better, and with synchronized methods (nice bonus).
This one looked promising: Generic event code - quite promising, but I kept on looking.

But your code is the best so far. I'm going to test it and will probably use it in my internal, little project.

Thanks a lot!

And here is a link to original code on GitHub: EventListeners.java

No comments: