[英]Change the directory of JFileChooser
我想记住用户第一次输入的目录,然后将默认目录设置为先前选择的目录。 我试图通过将静态变量存储为路径并将其传递给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;
}
}
尝试使用fileopen.getCurrentDirectory()
而不是file.getPath()
。 或者只是将您的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.