JFileChooser保存路徑的問題

字號(hào):

我們?cè)谟肑FileChooser時(shí),每次都默認(rèn)從"我的文檔"里打開,怎么才能讓它“記”住上一次的位置呢,這里有一個(gè)很簡單的方法,那就是將上次的路徑保存在注冊(cè)表里,然后在啟動(dòng)JFileChooser之前,將路徑從注冊(cè)表讀出來,再把它作為參數(shù)初始化JFileChooser,下面是具體代碼:
    Preferences pref = Preferences.userRoot().node("/com/lingyun");
    String lastPath = pref.get("lastPath", "");
    JFileChooser chooser = null;
    if(!lastPath.equals("")){
    chooser = new JFileChooser(lastPath);
    System.out.println("lastPath:" + lastPath);
    }
    else
    chooser = new JFileChooser();
    chooser.setFileFilter(new PdfFilter());
    chooser.showOpenDialog(this);
    File choosedFile = chooser.getSelectedFile();