2014-02-06 61 views
1

我有下面的代碼:JFileChooser中選擇豬病

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == btnNajitPDFCache) { 
     JFileChooser chooser; 
      String choosertitle = "Select directory."; 
     chooser = new JFileChooser(); 
     chooser.setCurrentDirectory(new java.io.File(".")); 
     chooser.setDialogTitle(choosertitle); 
     chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
     chooser.setApproveButtonText("OK"); 
     // 
     // disable the "All files" option. 
     // 
     chooser.setAcceptAllFileFilterUsed(false); 
     //  
     if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { 
      textFieldPDFCache.setText(chooser.getCurrentDirectory()+""); 
     } 
    } 
} 

這是確定的。我選擇打開選擇器窗體中的c:\ test文件夾,然後點擊按鈕確定。

但是chooser.getCurrentDirectory()只返回c:\。爲什麼?哪裏不對?

回答

2

您應該改用chooser.getSelectedFile()

0

您可以將規範路徑設置爲 文件Canonicalpath = new File(new File(「C:/」)。getCanonicalPath());

3

getCurrentDirectory()返回在 JFileChooser中打開的當前目錄。當你選擇C:\test你打開C:\目錄,所以你在getCurrentDirectory()

getSelectedFile()越來越C:\返回所選的文件(在你的情況下,該文件是目錄)。所以如果你想要用戶選擇的目錄使用getSelectedFile()

相關問題