• Post Reply Bookmark Topic Watch Topic
  • New Topic

Actions: Rules of Thumb

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there are several ways to deal with action events in Swing applications.

1. public class SomeClass implements ActionListener ...
2. button.addActionListener(new ActionListener() { ....
3. public class CopyAction extends AbstractAction...

What I am looking for are some rules of thunb as to when and why to use each of those 3 different options. I personally strive for #3 as much as possible but sometimes I just get get around needing to deal with class members not being available to the action. So #2 comes into play. I try and avoid #1 like the plague.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preference is #2. I have an easier time finding my way around that way. Any particular reason why anonymous inner class aren't your favorite?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marc Peabody:
My preference is #2. I have an easier time finding my way around that way. Any particular reason why anonymous inner class aren't your favorite?


Because they aren't resuable. Yes, I can create a method that several inner class actions uses. But it feels more, um, "standard" if you will to re-use the entire action.

vs
 
perry forman
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked kind of this question maybe one month ago without replies. Glad to see someone else ask it.

I'm working on my first Java project right now. The forms are rather complicated. And I think the code is made far more complicated by the use of inner classes.

I think it makes it far more complicated to debug the app, get an idea of the process flow by quickly looking at the source code, etc.

I found some swing examples elsewhere on the net where the author said to get used to inner classes as they are used liberally thru-out by Java programmers. But I just don't find that to be an acceptable statement.

I asked one of the better Java programmers the purpose of inner classes when .Net added the feature to the 2.0 release. His answer was that it allows for programmers to be lazy.

Does the book mentioned elsewhere, "Java Desktop Live" discuss methodologies for architecting complicated layouts?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My preferred way is

new JButton(new Action(...

If I find an opportunity to reuse the action, I can easily refactor it to a top level class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!