java - What is the appropriate place to include Action Listner Block -
okay, have 3 classes. 1.) program driver-main class 2.)toppanel 3.) frame action listener logout button working okay kind of thought maybe there's better way separate blocks action listeners. please bear me i'm new this. best way separate action listeners block? need implement action listener on class everytime or can same thing did here
here's code. toppanel class
public class toppanel extends jpanel{ //declaration jbutton logoutbutton = new jbutton("logout"); toptabbedpane toptabbedpane = new toptabbedpane(); private final border mylineborder = borderfactory.createlineborder(color.black, 2); //constructor public toppanel(){ setpanelinitialproperties(); addcomponents(); } //methods private void setpanelinitialproperties(){ setlayout(new gridbaglayout()); setborder(mylineborder); //sets line border panel //setbackground(color.red); } private void addcomponents(){ gridbagconstraints toptabbedpanegbc = new gridbagconstraints(); gridbagconstraints logoutbuttongbc = new gridbagconstraints(); toptabbedpanegbc.gridx = 0; toptabbedpanegbc.gridy = 1; toptabbedpanegbc.anchor = gridbagconstraints.center; this.add(toptabbedpane,toptabbedpanegbc); //adds tabbedpane holding home, administration... top panel logoutbutton.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e){ int logoutchoice = joptionpane.showconfirmdialog(null, "do want logout?"); if(logoutchoice == 0){ system.exit(0); } } }); logoutbuttongbc.gridx = 0; logoutbuttongbc.gridy = 0; logoutbuttongbc.anchor = gridbagconstraints.first_line_end; this.add(logoutbutton,logoutbuttongbc); } }
and here's frame class
public class topframe extends jframe { //declaration toppanel toppanel = new toppanel(); //create object //constructor 1: public topframe(){ setframeinitialproperties(); addcomponentstopane(); } //method 1: private void setframeinitialproperties(){ this.setlayout(new borderlayout()); this.setresizable(true); this.setpreferredsize(new dimension(1300,700)); //set it's dimensions or it's size this.setvisible(true); //sets it's initial visibility true shows on screen when objects created class this.setdefaultcloseoperation(jframe.exit_on_close); this.settitle("enrollment system"); //always put pack() first before setlocationrelativeto(null) this.pack(); this.setlocationrelativeto(null); } //method 2: private void addcomponentstopane(){ container mycontainer = this.getcontentpane(); //stores frame container named mycontainer mycontainer.add(toppanel); //adds 1 panel named toppanel } }
i'd appreciate explanation or examples.
thanks.
a lot comes down context.
with inclusion of inner , anonymous classes, making actionlistener
s has become way more easier, lambda expression can reduce lot of clutter (but imho can make harder read , ascertain api contracts).
in case, when contents of actionlistener
few lines long, anonymous class, have, more suitable, it's isolate , contextual (now imagine had dig out separate class see few lines :p)
larger more complex actions might require inner class, make code easier read, remains contextual (as non-static inner classes can access methods , fields of outer class), doesn't clutter code.
the time might consider using external class if functionality of actionlistener
re-usable in way, then, i'd use actions api, self contained units of work. problem here, need provide context them, passing reference of object contains methods/fields need work with, @ point, need make careful decisions design , interfaces
can help
so, based on current functionality, create "generic" "logout" action, example...
public class logoutaction extends abstractaction { private logoutaction parent; public closeaction(string name, component parent) { super(name); this.parent = parent; } @override public void actionperformed(actionevent e) { int logoutchoice = joptionpane.showconfirmdialog(parent, "do want logout?"); if (logoutchoice == joptionpane.ok_option) { swingutilities.windowforcomponent(parent).dispose(); } } }
and use like...
public class toppanel extends jpanel{ private jbutton logoutbutton; //... private void addcomponents() { //... logoutbutton = new jbutton(new logoutaction(this, "logout")); //...
and it, when button clicked, popup question , if user chooses ok, associated window closed (and if it's last window it's configured close on exit, jvm exit)
Comments
Post a Comment