• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Actions: Rules of Thumb

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic