This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Processing different ActionEvents in actionPerformed() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Processing different ActionEvents in actionPerformed()" Watch "Processing different ActionEvents in actionPerformed()" New topic
Author

Processing different ActionEvents in actionPerformed()

Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
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!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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: http://www.coderanch.com/t/454492
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
Interesting discussion.

Thanks very much for the reply!
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
You're welcome. Please let us know how your project works out.
Sebastiao Fernandes
Greenhorn

Joined: Aug 27, 2009
Posts: 13
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!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Aug 27, 2009
Posts: 13
I was using

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Processing different ActionEvents in actionPerformed()
 
Similar Threads
Event Handle..
Onload complex issue
Andy and Dave: Does GUI unit-testing exist?
Co-dependent Classes
Action Listener