swing - Java checkbox state in another class -
how pass actual checkbox state (true/false) gui class class? want run part of code if checkbox in gui selected. guess has if statement (highlithed part below) cant working.
public class csvtoxls { public static void main() throws ioexception { //here enter path directory. //for example: path workdir = paths.get("c:\\users\\kamil\desktop\\csvtoxlspython\\nowy folder (2)") jfilechooser jfc = new jfilechooser(filesystemview.getfilesystemview().gethomedirectory()); jfc.setdialogtitle("wybierz folder konwersji: "); jfc.setfileselectionmode(jfilechooser.directories_only); jfc.setacceptallfilefilterused(false); int returnvalue = jfc.showsavedialog(null); if (returnvalue == jfilechooser.approve_option) { if (jfc.getselectedfile().isdirectory()) { system.out.println("you selected directory: " + jfc.getselectedfile()); string z; //@suppresswarnings("deprecation") path workdir = jfc.getselectedfile().topath(); system.out.println(workdir); //path workdir = filesystems.getdefault(jfc.getcurrentdirectory()).jfc.getcurrentdirectory(); //path workdir = paths.get(gui.pickpath(jfc)); file dirg = jfc.getselectedfile(); //string str = dirg.getpath(); // ************* code issue ************* if textarealogprogram.checkbox.isselected() { try { thread.sleep(5000); //1000 milliseconds 1 second. } catch(interruptedexception ex) { thread.currentthread().interrupt(); } string str = dirg.getpath(); delfiles td = new delfiles(); td.deletefiles(str + "/", ".csv"); system.out.println("success!"); msgbox.infobox("succes!", "csvtoxls"); }
gui class:
public class textarealogprogram extends jframe { private jtextarea textarea; private jbutton buttonstart = new jbutton("convert"); private jbutton buttonclear = new jbutton("clear"); private printstream standardout; public textarealogprogram() { super("csvtoxls"); jcheckbox checkbox = new jcheckbox(); add(checkbox); checkbox.settext("delete files"); checkbox.setselected(true);
your other class need method or constructor parameter able accept value other class
see passing information method or constructor more details
other issues:
- your program structure needs redone completely. right main method large, meaning you're doing within static world , not using java best oops advantage.
- before thinking of creating gui, first create non-gui "model" classes program need. classes, these should have minimal static fields , methods, , strive follow object-oriented best practices
- you've got
thread.sleep
within gui code, not work swing gui's since risks putting entire gui sleep, making non-responsive. if want swing delays, use swing timer (google excellent tutorial on this) - you're trying check checkbox if static field of textarealogprogram class. it's not static field , in fact not field of class.
- the fact you're doing above suggests benefit studying introductory tutorials on object-oriented programming , java -- putting cart before horse trying create gui before first understanding java fundamentals. again, won't regret effort expended doing this.
- whatever do, don't make jcheckbox static field , try access way. lead spaghetti code , increased risk bugs.
- instead, make non-static (instance) private field of textarealogprogram class, , give class getter method allow other objects access jcheckgbox's state.
- there's more can mentioned code , problem... now.
Comments
Post a Comment