JFileChooser保存路徑

我們在用JFileChooser時,每次都默認從"我的文檔"裏打開,怎麼才能讓它“記”住上一次的位置呢,這裏有一個很簡單的方法,那就是將上次的路徑保存在註冊表裏,然後在啓動JFileChooser之前,將路徑從註冊表讀出來,再把它作爲參數初始化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();

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章