Action Listener
Event handlers, what should be done when a user performs certain operations
Action Event Examples
User clicks a button
Chooses a menu item
Presses Enter in a text field
Write Action Listener Steps
Declare event handler class either implements ActionListener interface or extends class that implements ActionListener interface Register instance of event handler class as a listener on component Include code that reacts to the action
Write Action Listener Code
public class MyClass implements ActionListener
{
someComponent.addActionListener(instanceOfMyClass);
public void actionPerformed(ActionEvent e)
{
//reaction code
}
}addActionListener method
Program registers object as action listener on button or event source
ActionEvent argument
Single argument gives information about event and its source
Action Listener Interface
actionPerformed(actionEvent)
Called just after the user performs an action
Action Event Class String getActionCommand()
Return string associated with this action
Action Event Class int getModifiers()
Returns integer represent modifier keys user was pressing on keyboard
Action Event Class Object getSource()
Returns obj that fired the event