Java ActionEvents and ActionListeners

an ActionListener is an interface for receiving action events, a class that should process action events should implement this interface, then be registered with a component.

an ActionListener can be registered with any object capable of it, typically this is an instance of an AbstactButton class like a JButton or JMenuItem.

 
 
this can be done by implementing the ActionListener on the parent of the AbstractButton object:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ActionListenerTest
        extends JFrame
        implements ActionListener {

    //define the ActionListener subject object
    private JButton button;
    //define the example target value
    private int numberOfClicks;

    public ActionListenerTest() {
        //set the example target value
        this.numberOfClicks = 0;

        //create the ActionListener subject object
        this.button = new JButton("OK");
        //register the ActionListener with the ActionListener subject object
        this.button.addActionListener(this);
        //add the ActionListener subject object to the content pane
        this.getContentPane().add(this.button);
        //"pack the frame for visual appeal
        this.pack();
    }

    //this is the method that the implementation of ActionListener forces us to have,
    //  this will be called by the component when an "Action" is performed
    public void actionPerformed(ActionEvent e) {
        //adjust the example target value
        this.numberOfClicks++;
        //echo the example target value
        System.out.println( "The button has been pressed "+this.numberOfClicks+" times.");
    }

    public static void main(String args[]){
       ActionListenerTest frame = new ActionListenerTest();
       frame.setVisible(true);
    }

}

 
 
this can also be done by implementing the ActionListener on an inner class of the parent of the AbstractButton object:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ActionListenerTest
        extends JFrame {

    private JButton button;

    public ActionListenerTest() {
        //create the ActionListener subject object
        this.button = new JButton("OK");
        //register the ActionListener with the ActionListener subject object
        this.button.addActionListener(new ActionListenerInnerTest());
        //add the ActionListener subject object to the content pane
        this.getContentPane().add(this.button);
        //"pack the frame for visual appeal
        this.pack();
    }

    private class ActionListenerInnerTest
            implements ActionListener {
        //define the example target value
        private int numberOfClicks = 0;
        public void actionPerformed(ActionEvent e) {
            //adjust the example target value
            this.numberOfClicks++;
            //echo the example target value
            System.out.println( "The button has been pressed "+this.numberOfClicks+" times.");
        }
    }

    public static void main(String args[]){
       ActionListenerTest frame = new ActionListenerTest();
       frame.setVisible(true);
    }

}

 
 
an advantage to the inner class method is that ic can be reused for similar situations:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ActionListenerTest
        extends JFrame {

    private JButton button1;
    private JButton button2;
    private JButton button3;

    public ActionListenerTest() {
        //define and set a layout for the content
        BoxLayout layout = new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS);
        this.getContentPane().setLayout(layout);
        //create the ActionListener subject objects
        this.button1 = new JButton("Button 1");
        this.button2 = new JButton("Button 2");
        this.button3 = new JButton("Button 3");
        //register the ActionListener with the ActionListener subject object
        this.button1.addActionListener(new ActionListenerInnerTest());
        this.button2.addActionListener(new ActionListenerInnerTest());
        this.button3.addActionListener(new ActionListenerInnerTest());
        //add the ActionListener subject objects to the content pane
        this.getContentPane().add(this.button1);
        this.getContentPane().add(this.button2);
        this.getContentPane().add(this.button3);
        //"pack the frame for visual appeal
        this.pack();
    }

    private class ActionListenerInnerTest
            implements ActionListener {
        //define the example target value
        private int numberOfClicks = 0;
        private String sourceText = null;
        public void actionPerformed(ActionEvent e) {
            //adjust the example target value
            this.numberOfClicks++;
            //set the source text if needed by casting event source to a JButton
            if( this.sourceText == null ) {
                this.sourceText = ((JButton )e.getSource()).getText();
            }
            //echo the example target value
            System.out.println( "The button: "+this.sourceText+" has been pressed "+this.numberOfClicks+" times.");
        }
    }

    public static void main(String args[]){
       ActionListenerTest frame = new ActionListenerTest();
       frame.setVisible(true);
    }

}

Leave a comment

  • Categories

  • Need Code Written?

  • Need a Coding Job?

  • Tags

  • Archives