• Post Reply Bookmark Topic Watch Topic
  • New Topic

One action listener for all JButtons or several versus an action listener for each button

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are your opinions based on the topic? Is it bad practice to use inner classes for action listeners? I was reading a article and it says

Specific objections. There are a couple of reasons for not using one single listener, but let's imagine a case where there are a lot of components, for example, MS Word. You can imagine the many hundreds of buttons, menu item, etc that generate ActionEvents (assuming it was written in Java).

•Doing sequential string comparisons is ugly, even if you don't worry about the inefficiency.
•It's hard to modularize a large GUI if everything goes to one listener.

It's like having all method calls in a class enter at one dispatcher that tested a string and decided with method to call. No one would stand for that kind of nonsense. It's trivial to have a separate listener for each action, and offensive to have everything come to one listener only to be divided into separate calls.

Better is to have a separate listener for each distinct action.


Article found here: Java BasicsL Listener Alternative
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both those quotes give good, object‑oriented advice. I have my own rule of thumb: look here. I have a slightly different opinion about anonymous inner classes from that in the second link; I think they are really useful for something you do once and once only. A listener which changes colours, if you change one Component green and another red, is not doing something once and once only; it is doing almost the same thing in two places, so you should not use an anonymous class.
Whether your Listener classes should be public or private depends on whether their actions are going to be of local use inside once class only, or of general use.

Others will have different opinions; if you ask Rob Spoor I think he will agree with the suggestion of using Actions throughout. He knows what he is doing.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Both those quotes give good, object‑oriented advice. I have my own rule of thumb: look here. I have a slightly different opinion about anonymous inner classes from that in the second link; I think they are really useful for something you do once and once only. A listener which changes colours, if you change one Component green and another red, is not doing something once and once only; it is doing almost the same thing in two places, so you should not use an anonymous class.
Whether your Listener classes should be public or private depends on whether their actions are going to be of local use inside once class only, or of general use.

Others will have different opinions; if you ask Rob Spoor I think he will agree with the suggestion of using Actions throughout. He knows what he is doing.


Thank you for the post as this entirely cleared things up......
 
Campbell Ritchie
Marshal
Posts: 52590
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!