NickG
NickG

Reputation: 901

JFileChooser: Cannot select Desktop when Selection Mode is File and Directories

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

Upvotes: 0

Views: 2108

Answers (2)

Kevin Day
Kevin Day

Reputation: 16383

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

Upvotes: 2

camickr
camickr

Reputation: 324108

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 );
            }
        }
    }
}

Upvotes: 2

Related Questions