Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[Event handling] Implementing ActionListener vs. anonymous inner function

 
Brian Lang
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I debated on whether this should go in the Swing forum and decided it was more a general modeling question than a technical GUI question, I'll leave that for you to decide.

At any rate, I typically use an anonymous inner function for event handling, e.g.


I've also seen classes that implement the ActionListener interface and of course require an actionPerformed() method to be implemented. So the above class could be written something like


..and so on. Of course this could apply to any component, AFAIK. What's the advantage to doing this vs. an anonymous class? I think I prefer having the control over the event right inside that particular JButton object, but would the other way offer more flexibility?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

There's actually a third option: define listeners as named classes that implement ActionListener, either nested or top-level.

I prefer to use anonymous listeners for one-liners, and small named classes for other listeners. Making the container implement ActionListener is really a hack, one that used to be necessary in the early days of Java before we had anonymous or named inner classes. I will personally never use it.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with your frame implementing ActionListener is that it can be used by other classes too.

I too always use anonymous classes or inner classes, the former much more than the latter. If the code in the listener becomes too large I usually create a method with all the code that then gets called by the listener.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Making the container implement ActionListener is really a hack . . .


It's not that good; I look on it as an abomination against object-oriented programming. Probably justifiable, however, when the Event concerns the Container directly eg WindowListener.

I have a rule of thumb:
  • If you might use the same Listener or one very similar in another situation: public class which implements ActionListener.
  • If you might use the same Listener or one very similar in several locations but always associated with the same top-level class: inner class.
  • If you are only going to use it once, and never use anything similar elsewhere: anonymous inner class.
  • Obviously you can think of situations where a package-private top-level class would be appropriate.
     
    Brian Lang
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the thought provoking replies.

    I thought as much (that it was a kludge or hack, if not just bad practice). It just doesn't "look right", and especially in code that I've seen implement this search for the event source it is downright ugly and does seem to break good object design.

    I also am aware of using a fleshed out implementation of ActionListener, just forgot to add it to my post. Although I am not very savvy with design patterns (other than singleton, and my grasp at MVC), I can see it being valuable in this regard.
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob often tells me to use Action instead of ActionListener; it allows you to add title, icon, listener etc to the button in a single instruction.
     
    Brian Lang
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:
    Rob often tells me to use Action instead of ActionListener; it allows you to add title, icon, listener etc to the button in a single instruction.


    Very cool, I had not seen that before.

     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can always learn something useful by reading Rob's posts. They are usually easy to find; they arrive 1 minute before mine.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic