What is "actionlistener"
An action event occurs whenever an action is performed by the user. For example, when the user clicks a button, chooses a menu item, or presses Enter in a text field. The result is that a message
through the method actionPerformed
is sent to all action listeners who are registered in the relevant component.
A class that aims to manipulate an action event, must implement the interface ActionListener
, and the object created with that class is registered to a component, using the method addActionListener
since component. When the action event occurs, the method actionPerformed
of this object is invoked, as the example below shows:
creating the implementing class
ActionListener
public class MyClass implements ActionListener { ... }
Registering an instance of the event handler class as a "listener" in one or more components:
someComponent.addActionListener(instanceOfMyClass);
Define what will occur when the action is executed within the method implemented by the class
ActionListener
:public void actionPerformed(ActionEvent e) { ...//Código que reage à ação... }
Already in this other example below, demonstrates how to implement a ActionListener
directly through the use of anonymous classes:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hey!");
}
});