[英]JFileChooser selected direcory
我有下一個代碼:
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:\\。 為什么? 怎么了?
getCurrentDirectory()
返回在JFileChooser
打開的當前目錄。 當您選擇C:\\test
,打開了C:\\
目錄,因此在getCurrentDirectory()
上獲得了C:\\
。
getSelectedFile()
返回選擇的文件(在您的情況下,該文件為目錄)。 因此,如果要用戶選擇的目錄,請使用getSelectedFile()
您應該改用chooser.getSelectedFile()
。
您可以將規范路徑設置為文件Canonicalpath = new File(new File(“ C:/”)。getCanonicalPath());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.