• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between EventHandler and ActionListener ?

 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find a good answer googling.What Is the difference between an ActionListener and an EventHandler ?






 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start one is used in Swing and the other in JavaFX.
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this discussion to the GUIs forum.

I think I was completely mistaken in my last post. Sorry. Event handler is a class which has factory methods allowing you to create listener objects.
Note that event handler appears to be an ordinary class and the listeners are usually interfaces which need to be implemented (although in some cases where they are functional interfaces you can use them in the form of a λ in Java8). Start by reading the documentation for event handler. It also says you are supposed to use event handler if you are writing IDEs and similar.
 
Campbell Ritchie
Sheriff
Posts: 51467
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JButton doesn't appear to have a setOnAction method (or if it has I missed it) so the second button is obviously the JavaFX version, which does have a setOnAction method.
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a confusing question - though that's not necessarily your fault. If could be made less confusing by mentioning what your target UI toolkit is (e.g. either JavaFX or Swing).

Campbell Ritchie is correct in: "For a start one is used in Swing and the other in JavaFX.". And that makes a lot of difference because the answer is different depending on the context. For instance, JavaFX has its own javafx.event.EventHandler which is different from the java.beans.EventHandler which Campbell Ritchie references.

Basically, my advice is not to mix the two toolkits (e.g. if you import javafx.anything, don't import javax.swing.anything or java.awt.anything or java.beans.anything). There are classes in JavaFX which do pretty much everything that can be done in those other packages and if you start mixing the imports of the the toolkits up you will end up with confusion.

Documentation on how to handle events in JavaFX is here:
http://docs.oracle.com/javase/8/javafx/events-tutorial/events.htm#JFXED117

If instead you are using Swing, then documentation for using listeners for that toolkit is here:
http://docs.oracle.com/javase/tutorial/uiswing/events/index.html

Basically, the lines of code you provided do pretty similar things (add a callback which can be invoked when something occurs - in this case a button is pressed), they are just for different APIs and toolkits.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Thanks for the Info.Sorry for mixing the javafx and swing.My question Is regarding the difference between ActionListener and EventHandler In general and not the specific button methods.
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ack, this is making my head spin.
Your question isn't about client GUI toolkits at all - neither Swing/AWT nor JavaFX.

The ActionListener url you reference: http://docs.oracle.com/javaee/7/api/javax/faces/event/ActionListener.html is a server side JavaEE API for Java Server Faces.
I have no experience or expertise in that area.

So your question has been moved to the wrong forum, and should probably be moved to a JavaEE/Java Server Faces forum.
 
Nikolas Nikolaou
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So ... Who's on first ?
 
John Damien Smith
Ranch Hand
Posts: 299
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a report icon at the top of the post.

I clicked on it to report the question to a moderator so that they can move the question to an appropriate forum.
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, the API links have been generated by the forum software.

Nikolas, please clarify your question with the fully qualified names of the classes you are asking about.
 
Tim Holloway
Saloon Keeper
Posts: 18367
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so how did this end up in the JavaServer Faces forum?

The EventHandler and ActionListener have different meaning depending on what platform you're using them in.

As a general abstraction, events have to be explicitly fired by something, whereas Listeners usually tap into a process and monitor it. Sort of the difference between the Service Locator and Inversion of Control paradigms.

In JSF, an ActionListener is a method that is invoked by JSF when a JSF action is triggered by the client's clicking on a commandButton or commandLink. However it's not a function I recommend, since the JSF action method allows the same thing but in a framework-independent (POJO) manner.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic