java JavaFX 目录选择器 - 如何从目录中获取路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47654918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:43:23  来源:igfitidea点击:

JavaFX Directory Chooser - How to get the path from directory?

javajavafxnullpointerexceptiondirectory

提问by Albatross

I would like to get the path from an Directory with the DirectoryChooser. My code looks like that:

我想使用 DirectoryChooser 从目录中获取路径。我的代码看起来像这样:

DirectoryChooser dc = new DirectoryChooser();
dc.showDialog(stage);
File f = dc.getInitialDirectory();
String s = f.getAbsolutePath();
System.out.println(s);

But that doesn't work, can somebody help me? The method getInitialDirectory() is always null

但这不起作用,有人可以帮助我吗?方法 getInitialDirectory() 始终为 null

回答by Abdul

It Should look something like this:

它应该是这样的:

DirectoryChooser directoryChooser = new DirectoryChooser();
File selectedDirectory = directoryChooser.showDialog(stage);

if(selectedDirectory == null){
     //No Directory selected
}else{
     System.out.println(selectedDirectory.getAbsolutePath());
}

more here: example of DirectoryChooser

更多信息:DirectoryChooser 示例