JFileChooser:选择模式为文件和目录时无法选择桌面

发布于 2024-10-07 14:12:16 字数 369 浏览 2 评论 0原文

我遇到了 JFileChooser 的问题,想看看是否有解决方法。

如果创建了 JFileChooser 并且 setFileSelectionMode 为 FILES_AND_DIRECTORIES,则当用户单击左侧(在 XP 中)的快捷按钮(例如“桌面”或“我的文档”)或下拉到“桌面”时,该字段不会放置在“文件名”JTextPane 中。当单击“选择/接受”按钮时,没有任何反应(因为 isDirectorySelected() 由于某种原因返回 false)。

重写approveSeletion不起作用,因为BasicFileChooser中的事件处理函数不会调用它。

我该如何制作才能选择桌面而无需手动导航到桌面,而是通过单击左侧的快捷方式?

谢谢

I ran into an issue with JFileChooser and wanted to see if there is a workaround.

If the JFileChooser is created and the setFileSelectionMode is FILES_AND_DIRECTORIES, when a user clicks a shortcut button on the left (in XP) such as Desktop or My Documents or drop down to Desktop, the field is not placed in the File Name JTextPane. And when clicking the "Select/Accept" button, nothing happens (because isDirectorySelected() returns false for some reason).

Overriding the approveSeletion does not work because the Event Handler function in BasicFileChooser does not call it.

How would I make it so the Desktop can be selected without having to navigate to it manually, but by clicking the shortcut on the left?

Thanks

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

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

发布评论

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

评论(2

君勿笑 2024-10-14 14:12:16

在 Windows 中,桌面不受文件系统中任何文件的支持 - 它是一个 shell 命名空间。因此,JFileChooser 确实无法返回给您任何内容。是的,我知道有一个文件夹包含用户的桌面 - 但请记住,桌面实际上显示为用户桌面和“所有用户”桌面文件夹的组合 - 加上 shell 添加但不是一部分的其他内容任何文件夹(如垃圾箱)。因此,返回代表“桌面”的 File 对象几乎是不可能的。

长话短说:问问自己为什么需要这样做 - 很可能您将深入本机代码领域,处理名称空间 PIDL 以及您可能不想涉及的各种麻烦(对于您的一生)我,我无法理解为什么 M$ 必须让这些东西变得如此难以使用)...

这是 Windows shell 命名空间的介绍,以便您了解所涉及的内容:

http://msdn.microsoft.com/en-us/library/cc144090%28v=vs .85%29.aspx

In Windows, the desktop is not backed by any file in the file system - it's a shell namespace. So there really isn't anything that JFileChooser could return to you. Yes, I know that there is a folder that contains the desktop for the user - but remember that the desktop actually displays as a composite of the user's desktop and the All Users desktop folder - plus other things that are added by the shell but not part of any folder (like the trash bin). So returning a File object that represents the 'desktop' is pretty much a non-starter.

Long and short: Ask yourself why you need to do this - chances are that you are going to wind up deep into native code territory, dealing with namespace PIDLs and all sorts of nastiness that you may not want to get into (for the life of me, I cannot understand why M$ had to make this stuff so amazingly difficult to use)...

Here's an intro to Windows shell namespaces so you'll have a feel for what's involved:

http://msdn.microsoft.com/en-us/library/cc144090%28v=vs.85%29.aspx

妄断弥空 2024-10-14 14:12:16

在BasicFileChooserUI中发现以下代码:

if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES 
&&  fc.getFileSystemView().isFileSystem(dir)) {
    setFileName(dir.getAbsolutePath());
}

所以看起来“特殊文件夹”被故意忽略。该代码位于私有方法中,因此很难创建自己的 UI。

作为黑客,您也许可以将 PropertyChangeListener 添加到文件选择器:

public void propertyChange(final PropertyChangeEvent e)
{
    String prop = e.getPropertyName();

    if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
    {
        JFileChooser fileChooser = (JFileChooser)e.getSource();
        File currentDirectory = (File)e.getNewValue();

        String directory = currentDirectory.toString();

        if (directory.endsWith("Desktop")
        ||  directory.endsWith("My Documents"))
        {
            File selectedFile = fileChooser.getSelectedFile();

            if (selectedFile == null || ! selectedFile.equals(currentDirectory))
            {
                fileChooser.removePropertyChangeListener( this );
                fileChooser.setSelectedFile( currentDirectory );
                fileChooser.addPropertyChangeListener( this );
            }
        }
    }
}

Found the following code in the BasicFileChooserUI:

if (fc.getFileSelectionMode() == JFileChooser.FILES_AND_DIRECTORIES 
&&  fc.getFileSystemView().isFileSystem(dir)) {
    setFileName(dir.getAbsolutePath());
}

So it looks like "special folders" are purposely ignored. The code is in a private method so it would be hard to create you own UI.

As a hack you might be able to add a PropertyChangeListener to the file chooser:

public void propertyChange(final PropertyChangeEvent e)
{
    String prop = e.getPropertyName();

    if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop))
    {
        JFileChooser fileChooser = (JFileChooser)e.getSource();
        File currentDirectory = (File)e.getNewValue();

        String directory = currentDirectory.toString();

        if (directory.endsWith("Desktop")
        ||  directory.endsWith("My Documents"))
        {
            File selectedFile = fileChooser.getSelectedFile();

            if (selectedFile == null || ! selectedFile.equals(currentDirectory))
            {
                fileChooser.removePropertyChangeListener( this );
                fileChooser.setSelectedFile( currentDirectory );
                fileChooser.addPropertyChangeListener( this );
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文