java - What is the difference between having common or separate actionPerfomed method -


please me understanding difference between adding action listener jcomponent in following 2 approaches.

first method: implementing actionlistener class , adding common actionperformed method choose selection based on 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 action listener 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 difference between these 2 approach , 1 more cleaner , maintainable approach , if there efficiency benefit involved ?

i'd go first approach if:

  • the action fired via different events. example, have action changes language of gui english arabic (where need re-arrange components lay right left) , action can fired via key bindings (alt + r) , via jmenuitem, , maybe via buttons.

  • several actions have same base code. example, calculator application each math operation button fire same action , based on action command can determine operation inside actionperformd(). share gui updates.

and i'd go second approach if:

  • the action tied 1 event , want write on fly.

what wouldn't similar this:

public class mainframe extends jframe implements actionlistener 

but write:

public class customlistener implements actionlistener 

also see:


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -