Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8336699

JFileChooser "Look In" doesn't work for Windows desktop non-special to special

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If a JFileChooser is navigated to the desktop folder on Windows (C:\Users\<username>\Desktop) as an ordinary file, then using the top level "Desktop" on the "Look In" menu won't work (it won't navigate to the special Desktop folder, which is not the same thing).

      The problem can be seen in the JFileChooser source code for the setCurrentDirectory(File) method:
                  /* Verify the toString of object */
                  if (this.currentDirectory.equals(dir)) {
                      return;
                  }
      The problem is that if currentDirectory is C:\Users\username\Desktop as a java.io.File and 'dir' is the special desktop folder as a sun.awt.shell.Win32ShellFolder2, they test equal but they are not the same thing - they don't have the same files list.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run code.
      Navigate to a non-Desktop folder, such as the root of the C drive.
      Type the desktop directory path C:\Users\<user-name>\Desktop into the "File Name" field and hit "Enter".
      Note the directories and files that are displayed.
      Use "Look In" to choose the top level "Desktop" (special) folder.
      Note that the same directories and files are displayed instead of the special folder ones - nothing changes.
      Use "Look In" to choose a non-Desktop directory, such as the root of the C drive.
      Use "Look In" to choose the top level "Desktop" (special) folder.
      Note that the correct directories and files are now displayed.

      This does not happen if clicking is used to navigate to the non-special desktop folder. In that case both the old and new directories will be special folders and they will not test equal even though they have the same path.

      So this is only a problem if "File Name" is used to navigate to the non-special desktop folder, or if the current directory is set to that folder programmatically.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expect "Look In" selection of top-level "Desktop" to always do the same thing - change the current directory to the special Desktop folder.
      ACTUAL -
      It doesn't work (nothing happens) if the current directory was the non-special Desktop folder and was set as described above.

      ---------- BEGIN SOURCE ----------
      import javax.swing.*;

      public class JFileChooserBug21 {
         public static void main(String args[]) {
            SwingUtilities.invokeLater(()->new JFileChooser().showOpenDialog(null));
         }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            tr Tejesh R
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: