2016-11-05 41 views
0

是否有可能直接在JFileChooser opendialog中打開預配置的目錄?如何使用JFileChooser直接打開目錄(位置)?

我試圖設置一些目錄與後續代碼:

File fn = new File("C://Users//me//Documents//Test"); 
    openFile = new JFileChooser(); 
    openFile.showOpenDialog(f); 
    openFile.setCurrentDirectory(fn); 
    fto = openFile.getSelectedFile(); 
    loadFile(openFile.getSelectedFile()); 
+0

的[我如何JFileChooser中在當前目錄下打開可能的複製的用戶在?](http://stackoverflow.com/questions/21844188/how-do-i-make-jfilechooser-open-in-the-current-directory-the-user-is-in) –

+0

謝謝,還有用的,但我正在尋找像下面的解決方案。儘管如此,我會考慮它。 –

回答

1

它可以去像這樣:

String startPath = "C://Users//me//Documents//Test"; 
JFileChooser openFile = new JFileChooser(new File(startPath)); 
openFile.showOpenDialog(null); 

File fileChoosen = openFile.getSelectedFile(); 
String fileName = openFile.getSelectedFile().getName(); 
String filePathAndName = openFile.getSelectedFile().getPath(); 

//Do what you want with the variables... 
System.out.println(fileName); 
System.out.println(filePathAndName); 
相關問題