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 |
|
|
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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access