java - JButton ActionListener general inquiry -
coming out of high school few years ago, i've been taught 2 ways add actionlistener
jbutton
. 1 way this:
jbutton mybutton = new jbutton("my button!"); mybutton.addactionlistener(new actionlistener(){ public void actionperformed(actionevent e){ //actions here } });
another way this:
jbutton mybutton = new jbutton("my button"); mybutton.addactionlistener(new mybuttonaction());
then somewhere @ bottom of code, add this:
class mybuttonaction implements actionlistener{ public void actionperformed(actionevent e) { //actions here } }
my question today is, there anyway can write sourcefile (a .java
) , write entire method in there? i'd want able perform actions original class
file, we'll call mybuttonclass
sake of example. able use variables int
double
string
, components properties
jprogressbar
jpanel
, jtextarea
original class , manipulate them , send them original class mybuttonclass
?
if so, mind helping me , shoving example? how call mybuttonclass
jbutton
?
let me know if questions misleading or unclear.
i think want :
public class myclass { public void mymethod() { jbutton mybutton = new jbutton("my button"); mybutton.addactionlistener(new mybuttonaction(this)); } public void anothermethod() { } }
and
public class mybuttonaction implements actionlistener{ private myclass mc; public mybuttonaction(myclass mc) { this.mc = mc; } @override public void actionperformed(actionevent e) { mc.anothermethod(); } }
you need pass class through contructor
Comments
Post a Comment