• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Processing different ActionEvents in actionPerformed()

 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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: http://www.coderanch.com/t/454492
 
Sebastiao Fernandes
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting discussion.

Thanks very much for the reply!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I was using

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic