Java 如何使用 JFileChooser 获取目录路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4019478/
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
How to get directory path using JFileChooser?
提问by Henry E. Wilson
I have a small java GUI application with a text field on it. When the user clicks the text field an event is triggered and the JFileChooser is launched. It's restricted to directories only.
我有一个带有文本字段的小型 Java GUI 应用程序。当用户单击文本字段时,将触发一个事件并启动 JFileChooser。它仅限于目录。
What I'm trying to do is to get the full path of the directory that was chosen and put it in the text field.
我想要做的是获取所选目录的完整路径并将其放入文本字段中。
I have no idea how to do this, I've searched through a ton of java tutorials and documentations and I can't find an answer. Can someone help me?
我不知道该怎么做,我搜索了大量 Java 教程和文档,但找不到答案。有人能帮我吗?
To clarify: I want to get the full path as a string and put it in the text field, overwriting anything that was there before.
澄清一下:我想以字符串形式获取完整路径并将其放入文本字段中,覆盖之前存在的任何内容。
采纳答案by Michael Myers
Try something like
尝试类似的东西
myTextField.setText(myFileChooser.getSelectedFile().getAbsolutePath());
What you are doing there is retrieving the File object from the file chooser, then grabbing its pathand throwing it into the text field.
您在那里所做的是从文件选择器中检索 File 对象,然后获取其路径并将其放入文本字段中。
回答by Entity
Check out the JFileChooser.getCurrentDirectory()
function:
查看JFileChooser.getCurrentDirectory()
函数:
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory())
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFileChooser.html#getCurrentDirectory())