java - Best way of opening a file (and ensuring file is chosen) -
i have 2 seperate methods of opening file.
the first uses filechoser additional file type filter.
jfilechooser infilename = new jfilechooser(); filenameextensionfilter filter = new filenameextensionfilter("pcf & txt files", "pcf", "txt"); infilename.setfilefilter(filter); component parent = null; int returnval = infilename.showopendialog(parent);` the second uses joptionpane has loop ensure directory chosen exists
string filepath; file directory; do{ filepath = joptionpane.showinputdialog("please enter directory"); directory = new file(filepath); if (directory.exists()==false){ joptionpane.showmessagedialog(null,"error directory"); } }while(directory.exists()==false); i'm looking best of both here. able choose file, using file filter , loop function should directory not valid.
i've tried switching around variable names , various functions in different places cant seem loop (".exists" function) work.
you need modify jfilechooser code use loop.
jfilechooser infilename = new jfilechooser(); file file; boolean valid = false; while (!valid) { int returnval = infilename.showopendialog(null); if (returnval == jfilechooser.approve_option) { file = infilename.getselectedfile(); valid = file.isdirectory(); else { valid = returnval == jfilechooser.cancel_option; } } its worth mentioning kind of thing might better achieved using;
jfilechooser.setfileselectionmode(jfilechooser.directories_only);
Comments
Post a Comment