In the List of event Listeners that are supported by the Java Swing, The Action Listener Interface is probably one of the most commonly used interfaces. The Action Listener can handle the events of many components. The Action Listener Interface has only one method that we have to implement. 

Action Listener Interface in Java

The ActionListener is suitable to implement by the following java components. Look at the List we have already explained some of these components. 

The Java Components that are Suitable to Implement Action Listener is,

  • button
  • check box
  • combo box
  • file chooser
  • formatted text field
  • menu item
  • password field
  • radio button
  • text field
  • toggle-button


The above are the swing components that can implement the Action Listener Interface. Remember Action Listener Interface has only one method that needs to be Implemented. 

We have already studied that how can we use the Event Listener. Click here to read. Now, let's start with each component of our list. What can we do with these Components? 



Java Swing Button Implements Action Listener

In Java Programming Swing Library, we have JButton class that let us create a Button. We can click on the Button, The click produces a click Event that we can Handle, We can register our button with Action Listener to Listen to the click Event in the Action Performed method of the Action Listener Interface. Look at the below code we registered our button to the Action Listener Interface, which will listen to the click of the button. You can copy the code and run it in your IDE.

pic
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    package AlixaProDev.EventListeners;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class ActionListenerOnButton extends JFrame{
        JButton button;
        ActionListenerOnButton(){
            // create button
            button = new JButton("click Me");
            // add button to the frame
            this.add(button);
    
            // Registering the button to the ActionListner
            button.addActionListener(new btnHanlder());
    
            // setting up the frame
            this.setTitle("AlixaProDev");
            this.setSize(new Dimension(300,300));
            this.setLayout(new FlowLayout());
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    
        // creating the handler class for the button
        private class btnHanlder implements ActionListener {
    
            @Override
            public void actionPerformed (ActionEvent e) {
                if (e.getSource()==button){
                    button.setText("Ok Got clicked");
                    button.setBackground(Color.MAGENTA);
                    button.setForeground(Color.BLUE);
                    button.setOpaque(true);
                }
            }
        }
    
        public static void main (String[] args) {
            new ActionListenerOnButton();
        }
    }



Java Swing CheckBox Implements Action Listener

The checkBox is another Java swing component that can implement the Action Listener Interface. In Java Swing, we have a class JCheckBox which lets us create the CheckBox. The CheckBox can Either be selected or not selected. We can trigger these two states or any other activity or event that can be performed by the Checkbox by Registering the CheckBox to the Action Listener Interface Handler. Look at the Below Example, You will understand the whole Scenario. If the user checks the checkbox then the button will be enabled otherwise the button will be disabled is you might have seen this scenario in many applications. 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package AlixaProDev.EventListeners;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ActionListenerOnCheckBox extends JFrame {
    JCheckBox checkBox;
    JButton button;
    ActionListenerOnCheckBox(){
        checkBox = new JCheckBox("I agree to the terms.");
        button = new JButton("Proceed");
        button.setEnabled(false);

        // Registering with the Handler
        checkBox.addActionListener(new CheckBoxHanler());
        this.add(checkBox);
        this.add(button);
        this.setTitle("AlixaProDev");
        this.setLayout(new FlowLayout());
        this.setSize(new Dimension(200,200));
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    // inner class for CheckBox Hanler
    private class CheckBoxHanler implements ActionListener{

        @Override
        public void actionPerformed (ActionEvent e) {
            if(e.getSource()==checkBox){
                button.setEnabled(checkBox.isSelected());
            }
        }
    }

    public static void main (String[] args) {
        new ActionListenerOnCheckBox();
    }

}


Summary for the Action Listener Interface 

If you are working with the Java swing components that are present in the above list that I have mentioned above, and you are doing some basic event Handling you can go with the Action Listener Interface. I recommend the Action Listener Interface for simple Event Handling. If you want to know more about Event Handlers.  Click here.

If you have any questions Please Let me know in the Comment Section.


Post a Comment

Previous Post Next Post