Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: adding an actionListener to a button, I need help!!
|
Posted: Mar 13, 2002 1:53 PM
|
|
If you have an applet or form with only one control that will respond to actions, this is okay, but if you have multiple actions defined this way, I would suggest using an (anonymous, usually) inner class for each. Here is an example of an anonymous inner class that implements the ActionListner interface being created and added to a button:
button.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent ae )
{
model.addElement( new String(textField.getText()) );
}
} );
I sometimes do this with named (instead of anonymous) inner classes; usually when I want to use the same action on more than one control, or when the class is so large that it makes the syntax of the inner class unwieldy.
|
|