JFileChooser 返回并非所有路径

发布于 2024-10-07 19:23:48 字数 849 浏览 0 评论 0原文

在“路径”按钮上使用以下方法单击:

public static void pathButtonAction() {
    JFileChooser chooser = new JFileChooser();
    if (pathToInbound == null) { //private static File pathToInbound;
    chooser.setCurrentDirectory(new java.io.File("."));
    } else {chooser.setCurrentDirectory(pathToInbound);
            }

    chooser.setDialogTitle("Choose folder with messages to send");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        pathToInbound = chooser.getCurrentDirectory(); 
        addLogText(chooser.getCurrentDirectory().getAbsolutePath());
    }


}

但这里我选择文件夹 c:\windows\temp 这里 addLogText(chooser.getCurrentDirectory().getAbsolutePath()) 我只能记录 c:\windows。为什么临时文件夹被忽略/截断?

using following method on Path button click:

public static void pathButtonAction() {
    JFileChooser chooser = new JFileChooser();
    if (pathToInbound == null) { //private static File pathToInbound;
    chooser.setCurrentDirectory(new java.io.File("."));
    } else {chooser.setCurrentDirectory(pathToInbound);
            }

    chooser.setDialogTitle("Choose folder with messages to send");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        pathToInbound = chooser.getCurrentDirectory(); 
        addLogText(chooser.getCurrentDirectory().getAbsolutePath());
    }


}

But here i choose folder c:\windows\temp
Here addLogText(chooser.getCurrentDirectory().getAbsolutePath()) i get to log only c:\windows. Why temp folder was ignored/truncated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时常饿 2024-10-14 19:23:48

您应该调用 chooser.getSelectedFile() 而不是 chooser.getCurrentDirectory(),这会返回用户在文件选择器中导航的当前目录。在您的情况下,它是C:\Windows

You should call chooser.getSelectedFile() instead of chooser.getCurrentDirectory(), this returns the current directory where the user has navigated in the filechooser. In your case it is C:\Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文