Change the directory of JFileChooser
I want to remember the directory what user entered first time and then set the default directory to previously chosen directory. I am trying to do this by storing a static variable as path and pass it to JFileChooser, but its not working can you tell me why , please:
public class BrowseInputUI {
public static String Path="";
public BrowseInputUI() {
JFileChooser fileopen = new JFileChooser(Path);//on second time user should see previous path
int ret = fileopen.showDialog(null, "Provide a file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
Path=file.getPath();
}
else if (ret == JFileCho开发者_如何学运维oser.CANCEL_OPTION){
Path=null;
}
}
public String GetPath(){
return Path;
}
}
Try fileopen.getCurrentDirectory()
instead of file.getPath()
. OR just make your filechooser as a class field:
public class BrowseInputUI
{
private JFileChooser fileopen = new JFileChooser();
public BrowseInputUI()
{
int ret = fileopen.showDialog(null, "Provide a file");
if(ret == JFileChooser.APPROVE_OPTION) File file = fileopen.getSelectedFile();
}
public String getPath()
{
return fileopen.getCurrentDirectory();
}
}
精彩评论