2011-10-05 43 views
1

我想记住用户第一次输入的目录,然后将默认目录设置为先前选择的目录。我想通过存储静态变量路径要做到这一点,并把它传递给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; 
    } 
} 

回答

4

尝试fileopen.getCurrentDirectory(),而不是file.getPath()。或者只是让你的文件选择器作为类字段:

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

+1为OR。这是最简单的解决方案。 –

+0

@Eng。 Fouad,我无法为fileopen.getCurrentDirectory返回String。 –