How Action Listener in Java Works

Introduction

This article explains Action Listener in Java. The Netbeans IDE is used for sample examples. In this article we describe the ActionListener interface, how to define it and then we use an example, using the Netbeans IDE, to understand how ActionListener works.

What is Action Listener

It is an interface used to handle the event tasks. It is easy to develop, maintain and implement. When we implement this interface in this class then we are working on some events such as the button clicked event, etcetera. For example, like the user presses Enter in a text field, clicks a button, chooses some components from a file menu, etcetera. Then the results are sent to the action listener that is generated for the relevant components.

How to declare

For declaring this interface we need to use the following procedure:

  • First, declare an event handler class and implements it as follows

          public class ClassName implements ActionListener {

  • Now register its component to the class by using "add".

          someComponent.addActionListener (instanceofClass);

  • Now write some code (in other words the task we need to perform) between braces. The syntax is as in the following:

          public void actionPerformed(ActionEvent ex)
          {
          //task to perform through this interface
          }

To determine where the user clicked on the screen, Java provides an interface called "ActionListener" through which we determine where the user clicked and generates an event to perform several tasks, like calculation, print a value, print a specific character, etcetera using a button.

Example

Let's use the Netbeans IDE to create a program in which we display how many times a user has clicked a button.

For this we need to do several steps.

Step 1

Opens the Netbeans IDE.

Fig-1.jpg

Step 2

Click on the "File" Menu then select "New Project" as in the following:

fig-2.jpg

Step 3

Choose "Java" -> "Java Application" as in the following:

fig-3.jpg

Step 4

Now click on "Next". A new window is generated. Now provide a project name (I used "ActnLstnr1") as in the following:

fig-4.jpg

Step 5

Now click on "Finish". Your project is now set up with the default file "ActnLstnr1.java" in "actnlstnr1 package" as in the following:

fig-5.jpg

Step 6

Now provide the following code in the "ActnLstnr1.java" file.

/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package actnlstnr1;

import java.awt.*;

import java.awt.event.*;

//use swing for swing components like button, textfield

import javax.swing.*;

public class ActnLstnr1 extends JFrame implements ActionListener

  {

    //Declare component which are to be displayed...

    private int i=0;

    JTextField text;

    JButton btn;

    //define constructor to add components to the frame and set frame properties in this

    public ActnLstnr1()

      {

        add(text=new JTextField(20));

        add(btn=new JButton("Click me"));

        setLayout(new FlowLayout());

        setSize(630, 320);

        setTitle("ActionListener Example");

        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn.addActionListener(this);

      }

    //Now implement ActionListener to achieve our goal

    public void actionPerformed(ActionEvent e)

      {

        i++;

        text.setText("Button clicked " +i+ " times");

      }

    //Declare main method

    public static void main(String[] args)

      {

        ActnLstnr1 ab=new ActnLstnr1();       

      }   

  }

fig-6.jpg

fig-7.jpg

Step 7

Now run your "ActnLstnr1.java" Java file. Right-click on the project menu and then select "Run" from the list as in the following:

fig-8.jpg

Step 8

Now the following output is generated with this code.

fig-9.jpg

Step 9

Now when you click on the button "ActionEvent" is generated in which we define a loop that increments the count of button clicks and prints that in the text field. The action is generated when we click for the first time as in the following:

fig-10.jpg

Now when we click the button four times it shows the number of clicks as in the following:

fig-11.jpg

Thanks For Reading


Similar Articles