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?
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.
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.
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.