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:
- best practice implementing actionlistener
- best practice gui , actionlistener
- actionlistener best practices
- nested class vs implements actionlistener
- organize code gui , actionlistener in java
- define button actions in inner class vs define button actions in public class in swing
- what better use: action vs actionlistener?
Comments
Post a Comment