简体   繁体   中英

How to get directory path using JFileChooser by selecting a folder not a file

I want to use the JFileChooser in Java to get the user to select where they would like to save a file. I have used this code:

JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        String filename = f.getAbsolutePath();

This works and does get the path however the dialog box requires the user to select a file in order to get the file path. As i want it to save a new file i need it to get the path without having to select a file but from the folder the user has selected instead.

Im new to this and not sure of any other way of doing this, please could you advise me on a way around this.

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM