开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜