4

Please help me understanding the difference between adding the action listener to JComponent in following two approaches.

First Method: Implementing actionListener to my class and adding the common actionPerformed method which choose selection based on the events

class Test implements ActionListener  
{
    JButton jbutton = null;
    public Test(){
        jbutton = new JButton();
        jbutton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        //Perform operation here;
    }
}

Second Method: Defining the action listener for individual JComponent.

JButton jbutton = new JButton();              
button.addActionListener(new ActionListener() {         
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        //Perform operation here    
    }
});

What is the difference between these two approach and which one is more cleaner and maintainable approach and if there is any efficiency benefit involved ?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ashish
  • 14,295
  • 21
  • 82
  • 127
  • 4
    Forget about efficiency. This is about [separation of concerns](http://en.wikipedia.org/wiki/Separation_of_concerns), clean code, maintainability, etc. – Matt Ball May 13 '13 at 17:52
  • @Matt Ball Thank you sir, I will read that article – Ashish May 13 '13 at 17:54
  • Fully agree with @MattBall. For desktop Java, go with the second approach for reasons of clarity, cleanliness and maintainability. If you were to ask the same question for Android, I might pick the first option for reduced GC load. – Barend May 13 '13 at 18:00
  • @Barend does second approach add any load on GC ? – Ashish May 13 '13 at 18:05
  • I'd suggest to use inner anonymous methods, debugging inside methods, code is easier – mKorbel May 13 '13 at 18:07
  • @Ashish it creates more objects, so yes. On a desktop JVM, this is a non-issue, because the hardware is so fast and the JVM so well optimized. Please don't "optimize" it unless you have specific, repeatable benchmarks indicating that it's what's slowing your app down. On Android, especially when you're supporting all the way back to Eclair like I am at work right now, this trade-off may fall differently. If I were targeting Ice Cream Sandwich and higher, I probably wouldn't even bother. – Barend May 13 '13 at 18:15

1 Answers1

3

I'd go with the first approach if:

  • The action is fired via different events. For example, you have an action that changes the language of the GUI from English to Arabic (where you need to re-arrange the components to lay from right to left) and that action can be fired via some key bindings like (Alt + R) and via a JMenuItem, and maybe via some buttons.

  • Several actions have the same base code. For example, a calculator application where each math operation button would fire the same action and based on the action command you can determine the operation from inside actionPerformd(). They share the GUI updates.

and I'd go with the second approach if:

  • The action is tied to only one event and you want to write it on the fly.

What I wouldn't do is something similar to this:

public class MainFrame extends JFrame implements ActionListener

but I would write:

public class CustomListener implements ActionListener

Also see:

Community
  • 1
  • 1
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417