Java find object reference -
i have panel more buttons of buttonclass different names. how can find out when click finish button on button clicked first(which object of buttonclass) , modify name ?
public class buttonclass extends jbutton{ public string name; public buttonclass(string name){ this.name = name; this.addactionlistener(new buttonlistener()); } public class buttonlistener implements actionlistener { public void actionperformed(actionevent e) { jframe frame = new jframe(); jbutton finish = new jbutton("finish"); finish.addactionlistener(new finish()); panel.add(finish); frame.add(panel); frame.setfocusable(false); frame.setsize(1600,900); frame.setlocationrelativeto(null); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.dispose_on_close); } } }
and other actionlistner trying create finish button :
public class finish implements actionlistener { public void actionperformed(actionevent e) { //here } }
so have buttonclass button1 = new buttonclass("button1"); button2, etc ;
when click on 1 of them , frame open jbutton called finish . when click on want modify name of buttonclass clicked in first place.
you modify action listener class keep reference button, like:
public class finish implements actionlistener { buttonclass button; public finish( buttonclass button ) { this.button = button; } public void actionperformed(actionevent e) { // can use field button when click } }
of course, you'll have modify button preparation:
jbutton finish = new jbutton("finish"); finish.addactionlistener(new finish((buttonclass)e.getsource()));
note i'm passing e.getsource()
finish
constructor. gives source of click in buttonlistener
, , pass down finish
object.
Comments
Post a Comment