如何使用JFileChooser获取目录路径? [英] How to get directory path using JFileChooser?

查看:152
本文介绍了如何使用JFileChooser获取目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本字段的小型Java GUI应用程序。当用户单击文本字段时,将触发事件并启动JFileChooser。它仅限于目录。

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.

我要做的是获取所选目录的完整路径并将其放在文本字段中。

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.

我不知道如何做到这一点,我搜索了大量的java教程和文档,但我找不到答案。有人可以帮助我吗?

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?

澄清一下:我希望将完整路径作为字符串并将其放在文本字段中,覆盖之前的任何内容。

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.

推荐答案

尝试类似

myTextField.setText(myFileChooser.getSelectedFile().getAbsolutePath());

你在做什么从文件选择器中检索File对象,然后抓住它的路径将其扔进文本字段

这篇关于如何使用JFileChooser获取目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆