java - Cannot set variable by listener -
i'm origin gui , listeners. want take , set file pc "file zvolenysubor" in class hlavna class implementing actionlistener. can take file in listener's actionperformed method , set "file subor" fail in saving "file zvolenysubor" in main (and of ideas how failed too). should change/add there please?
here classes:
public class hlavna { public static void main(string[] args) { file zvolenysubor = null; jframe frame = new jframe("abcde"); jbutton vybersuboru = new jbutton("vyber"); vybersuboru.setbounds(220, 15, 200, 20); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(640, 480); frame.add(vybersuboru); frame.setlayout(null); vyberlistener list1 = new vyberlistener(zvolenysubor); vybersuboru.addactionlistener(list1); vybersuboru.settext("vyber subor"); } } public class vyberlistener implements actionlistener { private file subor; public vyberlistener(file subor){ this.subor = subor; } @override public void actionperformed(actionevent e) { jfilechooser chooser = new jfilechooser(); system.out.println("lol"); filenameextensionfilter filter = new filenameextensionfilter("jpg & gif images", "jpg", "gif"); chooser.setfilefilter(filter); chooser.showopendialog(null); subor = chooser.getselectedfile(); system.out.println(subor.getabsolutepath()); } }
not sure you're asking here. java passes method arguments value opposed reference, file parameter you're supplying constructor of vyberlistener updated in vyberlistener class actionperformed method, not reference in main method. you're trying accomplish here?
if you're trying update "file zvolenysubor" in main method, may worthwhile making "file subor" in vyberlistener class public, can accessed main method.
update minimise amount of chatter in comments below, i'll seek , summarise. although java supports pass reference, it's passing pointers reference. in example, pass null object reference (subor) actionlistener, overwritten in actionperformed method:
subor = chooser.getselectedfile();
thus file variable "zvolenysubor" never updated. in order update fields in hlavna class, recommend using next "container" pattern:
public class hlavna { public static void main(string[] args) { // new "filereference" container instance filereference zvolenysubor = new filereference(); jframe frame = new jframe("abcde"); jbutton vybersuboru = new jbutton("vyber"); vybersuboru.setbounds(220, 15, 200, 20); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(640, 480); frame.add(vybersuboru); frame.setlayout(null); vyberlistener list1 = new vyberlistener(zvolenysubor); vybersuboru.addactionlistener(list1); // retrieves updated file instance file updatedfile = zvolenysubor.getfile(); } } public class vyberlistener implements actionlistener { private filereference subor; public vyberlistener(filereference subor){ this.subor = subor; } @override public void actionperformed(actionevent e) { jfilechooser chooser = new jfilechooser(); filenameextensionfilter filter = new filenameextensionfilter("jpg & gif images", "jpg", "gif"); chooser.setfilefilter(filter); chooser.showopendialog(null); subor.setfile(chooser.getselectedfile()); } } // filereference container class allows file reference updated actionlistener // without re-initialising object reference public class filereference { private file _file; public void setfile(file file) { _file = file; } public file getfile() { homecoming _file; } }
java actionlistener
No comments:
Post a Comment