Get the path of a directory using JFileChooser

后端 未结 3 983
甜味超标
甜味超标 2020-12-19 05:19

How can I get the absolute path of a directory using JFileChooser, just selecting the directory?

3条回答
  •  粉色の甜心
    2020-12-19 05:54

    JFileChooser's getSelectedFile() method, returns a File object. Use the getAbsolutePath() to get the absolute name to the file.

    modified example from the javadoc:

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this directory: " +
            chooser.getSelectedFile().getAbsolutePath());
    }
    

提交回复
热议问题