ActionEvent Class

Package: java.awt

An instance of the ActionEvent class is passed to the ActionListener when an action event occurs. You can use this object to determine information about the event.

Method

Method

Description

object getSource()

Returns the object on which the event occurred

You can use the getSource method to determine which component sourced the event when the listener is registered as an event listener with more than one component. For example:

private class ClickListener

implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == button1)

{

// code to handle button1 click

}

if (e.getSource() == button2)

{

// code to handle button2 click

}

}

}

In this example, the private class ClickListener can be registered with two buttons (button1 and button2). The getSource method is used in the actionPerformed method to determine which button was clicked.

CrossRef.eps For more information, see Event Handling.

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.