簡體   English   中英

更改JFileChooser的目錄

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM