Java ActionListener
When you click a button or menu item, the Java ActionListener gets alerted. It receives a warning about an ActionEvent. The java.awt.event package contains the ActionListener java interface. It just has one method, called actionPerformed(). This article will teach us more about ActionListener java, including events and certain examples.
Introduction to ActionListener Java
You must specify what an action listener can accomplish for the user's activity as a programmer. Consider a straightforward scenario in which a user clicks on a specific item in the menu bar or presses Enter to start a new line in a text field. A message titled action performed is sent to each action listener specified in the applicable component as soon as such user functions are completed.
The most important and fundamental component is an object that can implement the ActionListener in the java interface. The application must recognise this object as an action listener on the button, which is nothing more than an event source.
As a result, when the user hits the button, an action event is fired using the addActionListener method. This is called the actionPerformed method of the ActionListener in Java. Please be aware that the ActionListener in java interface only has one function. A single ActionEvent object supplied to the method details the event and its source.
The implements keyword in the class declaration allows for using an ActionListener in Java. Developing a new class that implements it can also be utilised independently of the class. It needs to be imported into your project as well. For JButtons, ActionListeners in java are typically used.
The ActionListener in java writing format is shown in the following pictures:
let's understand this better with a step-by-step example
Procedure : The steps to implement ActionListener in java are as follows:
Step 1:
Create an event handler class that either implements an ActionListener interface or extends a class that implements an ActionListener interface. Let’s create the first java file with all the frames and other components.
Step 2:
Register an instance of the event handler class as a listener on one or more components.
Step 3:
Include code that implements the methods in the listener interface. For example:
Step 4:
Now create the file with the actionPerformed(ActionEvent o) function of ActionListener in java.
Let's see the full example now in java.
Output: The above program will be generated by the ActionListener class in Java; it is the driver class.
actionPerformed() Method
Clicking on the registered component automatically calls the actionPerformed() method.
When you click a button or menu item, the ActionListener in Java is alerted and receives a warning about an ActionEvent.
Now let's see this method in action with the aid of a Java example,
Explanation :
Before you can utilise an action listener, a class must implement it. Just include the listener and the implements keyword.
Specifies that the component button will be included in the tracked components for an action event. An action listener in Java should be added to a component so that you can run programmes whenever a user clicks it. A component won't be observed if it doesn't have an action listener.
We have merely replaced a method already defined in the ActionListener Java class. Here, you'll specify what your software will do when a user clicks a component being watched for action events.
The Action Event Class
Let's now take a look at Action Event class methods in an elaborate way The Action Event Class
Methods | Description |
---|---|
String getActionCommand() | Returns the string that corresponds to this action. The setActionCommand method, supported by most objects that may fire action events, enables you to set this string. |
int getModifiers() | The button the user pushed during the action event is returned as an integer. The keys pressed are identified using a few ActionEvent-defined constants, such as SHIFT MASK, CTRL MASK, META MASK, and ALT MASK. For instance, the expression is nonzero if a user chooses a menu item. |
Object getSource()(in java.util.EventObject) | returns the event's event-firing object. |
Java ActionListener Examples
Implement ActionListener
Using the ActionListener in java is a typical strategy. There are three stages to implementing the ActionListener in java class:
1) Implement ActionListener interface in the class:
2) Register the component with the Listener:
3) Override the actionPerformed() method:
On Button click
Let's see another example of implementing ActionListener in the Java method.
Using Anonymous Class
The anonymous class can also implement the ActionListener in java. Because it is a shortcut, you do not need to complete the three steps:
Let us see the full code of ActionListener in Java using an anonymous class.
Conclusion
- The java.awt.event package contains the ActionListener in java interface.
- A single ActionEvent object supplied to the method details the event and its source.
- The application must recognise An object that can implement the Action Listener in java interface as an action listener on the button, which is nothing more than an event source.
- The keys pressed are identified using a few ActionEvent-defined constants, such as SHIFT MASK, CTRL MASK, META MASK, and ALT MASK. For instance, the expression is nonzero if a user chooses a menu item.
- There are three stages to implementing the ActionListener in java class: Implement the ActionListener in the Java interface in the class, Register the component with the Listener and Override the actionPerformed() method.