开发者

Change the directory of JFileChooser

开发者 https://www.devze.com 2023-04-10 13:30 出处:网络
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

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();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号