• 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

Processing different ActionEvents in actionPerformed()

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

What are good ways to program different event actions?

I am creating my first GUI and there are several action events that can occur in my GUI.
Right now I'm using ActionEvent.getSource() and an if block to differentiate between the events and process them accordingly.

I imagine there is a better way to handle the different events that can occur but I'm not sure what that is.

Any suggestions and/or web-sites to look at?

Thanks!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been discussed a lot here and on other Swing fora for the simple reason that it is a common question. My own opinion is that you should avoid using a switchboard actionPerformed method and try to create separate ActionListeners for distinct types of actions. For example: https://coderanch.com/t/454492
 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting discussion.

Thanks very much for the reply!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. Please let us know how your project works out.
 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Currently I am using the switchboard methodology. Everything is working but I think I will try your (pete's) "1 listener per action type".

Pete, just so I am clear, this methodology involves 1 implementation of ActionListener [and thus 1 ActionPerformed()] for each kind of action type, correct? That is, to use your calculator example in the link you added, you would have 2 implementations of ActionListener: 1 for numbers and 1 for operation buttons (+, -, *, /)?

How would I register the correct listener implementation (ButtonListener or OperationListener) to each button in the calculator?


Thanks again!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sebastiao Fernandes wrote:How would I register the correct listener implementation (ButtonListener or OperationListener) to each button in the calculator?


How were you "registering" the ActionListener when you had a 'switchboard' approach?
 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was using

 
reply
    Bookmark Topic Watch Topic
  • New Topic