Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should your class implement ActionListener or use an object of an anonymous ActionListener class

What's the best way for implementing the java.awt.event.ActionListener interface?

Have your class implement ActionListener and add this as an ActionListener:

class Foo implements ActionListener{

    public Foo() {
        JButton button = new JButton();
        button.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {

    }
}

Or add an object of an anonymous ActionListener class:

class Foo{

    public Foo() {
        JButton button = new JButton();
        button.addActionListener(new ActionListener() {     
            public void actionPerformed(ActionEvent e) {

            }
        });
    }
}
like image 312
elias Avatar asked Jul 11 '12 17:07

elias


People also ask

Why do we implement ActionListener in Java?

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.

What is the purpose of ActionListener interface in swing?

The class which processes the ActionEvent should implement this interface. The object of that class must be registered with a component. The object can be registered using the addActionListener() method.

What is ActionListener used for?

ActionListener in Java is a class that is responsible for handling all action events such as when the user clicks on a component. Mostly, action listeners are used for JButtons. An ActionListener can be used by the implements keyword to the class definition.


2 Answers

Some (jeanette/kleopatra) say to almost never use ActionListener, and to instead use Actions such as an AbstractAction. It's almost always a bad ideal to have your GUI class implement your listeners though as this breaks the Single Responsibility Principle and makes your code more difficult to maintain and extend,and so I strongly urge you not to do that.

So for example, an inner class for this:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;

class Foo {

   public Foo() {
       JButton button = new JButton(new ButtonAction("Action", KeyEvent.VK_A));
   }

   private class ButtonAction extends AbstractAction {
      public ButtonAction(String name, Integer mnemonic) {
         super(name);
         putValue(MNEMONIC_KEY, mnemonic);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         System.out.println("button pressed");
      }
   }

}
like image 188
Hovercraft Full Of Eels Avatar answered Sep 19 '22 12:09

Hovercraft Full Of Eels


The second option (anonymous class) is certainly better, another option would be to have a nested class within Foo.

I wouldn't go with the first option for two reasons:

  • The users of Foo shouldn't have to know that it implements ActionListener.
  • You cannot implement two different listeners in the same class.
like image 24
casablanca Avatar answered Sep 19 '22 12:09

casablanca