繁体   English   中英

使用JFileChooser获取目录的路径

[英]Get the path of a directory using JFileChooser

如何仅使用目录来使用JFileChooser获取目录的绝对路径?

采用:

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

和...一起:

chooser.getCurrentDirectory()
//or
chooser.getSelectedFile();

然后在返回的File对象上调用getAbsoluteFile()

JFileChoosergetSelectedFile()方法返回一个File对象。 使用getAbsolutePath()获取文件的绝对名称。

来自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());
}

尝试:

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();

System.out.println(fullPath);

fullPath为您提供Selected目录所需的绝对路径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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