aspose file tools*
The moose likes Beginning Java and the fly likes [Event handling] Implementing ActionListener vs. anonymous inner function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "[Event handling] Implementing ActionListener vs. anonymous inner function" Watch "[Event handling] Implementing ActionListener vs. anonymous inner function" New topic
Author

[Event handling] Implementing ActionListener vs. anonymous inner function

Brian Lang
Ranch Hand

Joined: Oct 21, 2008
Posts: 43
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

    Joined: Oct 21, 2008
    Posts: 43
    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

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    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

    Joined: Oct 21, 2008
    Posts: 43
    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

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    You can always learn something useful by reading Rob's posts. They are usually easy to find; they arrive 1 minute before mine.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: [Event handling] Implementing ActionListener vs. anonymous inner function