wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes ActionPerformed() and ActionListener() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ActionPerformed() and ActionListener()" Watch "ActionPerformed() and ActionListener()" New topic
Author

ActionPerformed() and ActionListener()

Hanawae Velez
Greenhorn

Joined: Jun 07, 2005
Posts: 1
Now to make the GUI do stuff. My question is, in all of my other classes, i have already written fucntions that will be tied to buttons (ie, add user[writes to db], delete user, etc), but my question lies with how do i properly implement actionListener and ActionPerformed.

Ive been looking at this tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html

public class Beeper ... implements ActionListener {
...
//where initialization occurs:
button.addActionListener(this);
...
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
}
}

my question is, since i have four buttons, am i supposed to have a seperate actionPerformed() version for each button? Or Can i write one huge actionPerformed() that tells java what to do with all of my buttons (in tieing the buttons to my functions)...? Any help is greatly appreciated.

~Hana
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
you can do it either way, actually. if your actionPerformed() is very short, you might be able to do an anonymous inner class for each of the buttons; see this story or this snippet from the middle of one of the Java tutorials for more details on this way of doing it.

or you can have just a single actionPerformed(), too, if you please. but then you'll likely need some way for that one method to tell exactly which button was pushed, in order to do different things for different buttons. there are several ways of doing this, but all of the useful ones rely on the argument that actionPerformed() is passed; look into it.
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
When I need to figure out which object should be the action listener for a component of for a bunch of components, I give priority to which class owns the data and methods that will be needed for event processing. That's not exactly conscious o-o design but it almost always gives me the right answer.

Here's an example. I recently wrote a program that draws fractals. There's a JFrame containing some buttons, as well as a FractalPanel that does the math and graphics. Two of the buttons are "ZOOM IN" and "ANIMATE". So the overall structure looks like this:



Zooming in and animating require the JFrame to do some stuff (like enabling/disabling some components, plus displaying some info) and then kicking the FractalPanel. So it seems clear that the code that handles clicks in the buttons should live in FractalFrame. Otherwise, if a separate class were the action listener, I would have to pass a bunch of info into that separate class, and that means duplication of data and inappropriate coupling.

Since I won't use an external class as a listener, I'm down to two choices: create an inner class, or have FractalFrame be its own listener.

I don't like creating an inner class in the situation because there's no benefit. An inner class would be handy if there were some superclass whose finctionality I'd like to inherit, but that's not the case here. So my FractalFrame catches its own action events.

Whenever a class is a listener to multiple sources of the same event type, there's te need to figure out which component actually got tickled. That's done by calling the event's getSource() method. Here's the evolved FractalFrame:



For more on this, including a nifty animated illustration that demonstrates the event handling mechanism of a component/listener arrangement that you design, see Chapter 16 of "Ground-Up Java".

Hope this helps.


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionPerformed() and ActionListener()