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
JavaFX Directory Chooser - How to get the path from directory?
提问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 示例