[英]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:
我试图通过将静态变量存储为路径并将其传递给JFileChooser来实现此目的,但是它无法正常工作,请告诉我原因,请:
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 == JFileChooser.CANCEL_OPTION){
Path=null;
}
}
public String GetPath(){
return Path;
}
}
Try fileopen.getCurrentDirectory()
instead of file.getPath()
. 尝试使用
fileopen.getCurrentDirectory()
而不是file.getPath()
。 OR just make your filechooser as a class field: 或者只是将您的filechooser作为类字段:
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.