JDK-8041694 : JFileChooser removes trailing spaces in the selected directory name
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7u25,7-pool,8-pool,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • Submitted: 2013-11-14
  • Updated: 2016-06-23
  • Resolved: 2016-05-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b124Fixed
Related Reports
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
1) java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
2)java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) Server VM (build 24.45-b08, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
1) Microsoft Windows [Wersja 6.1.7601]
2) Linux pc-krb1-20-239.ericpol.int 2.6.32-279.11.1.el6.i686 #1 SMP Tue Oct 16 11:16:53 CDT 2012 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
If a directory, whose name ends with space characters, is selected in "javax.swing.JFileChooser" object, the call to "JFileChooser.getSelectedFile()" method returns "java.io.File" object representing a path to the selected directory but without any trailing spaces in its name.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Code:
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(data.getFrame()) == JFileChooser.APPROVE_OPTION)
{
    File f = fc.getSelectedFile();
    boolean exists = f.exists();
}
Action:
In shown JFileChooser choose the folder that contains spaces in the end of name (for example "Z:\123  " on Windows or "/local/123  " on linux) and observe by means of the debugger that "fc.getSelectedFile();" returns "java.io.File" object corresponding to "Z:\123" or "/local/123" paths without trailing spaces.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
f points on correct file ("Z:\123  " or "/local/123  ")
and exists == true.
ACTUAL -
f points on file which name is without whitespaces ("Z:\123" or "/local/123")
and exists == false.

REPRODUCIBILITY :
This bug can be reproduced always.
Comments
The bug consists of 2 independent issues: ISSUE #1: The method "JFileChooser.getSelectedFile()" returns "java.io.File" object which does not contain trailing spaces in the directory name, in spite of the fact that the corresponding directory in the file system has trailing spaces in its name. The issue #1 is caused by the code in the method "javax.swing.plaf.basic.BasicFileChooserUI.ApproveSelectionAction.actionPerformed" which deliberately removes any trailing spaces from the string representation of the directory or file name. This issue is similar to the analogous bug JDK-6351074 about removing of leading spaces in the file name which was resolved in 2011. The solution for this bug is deletion of the code from the class "javax.swing.plaf.basic.BasicFileChooserUI" which modifies the exact directory or file name by removing the trailing spaces from their names. It was practically proven that after this solution is applied the method "JFileChooser.getSelectedFile()" returns "java.io.File" object which contain all trailing space characters in the directory name. ISSUE #2: The method "java.io.File.exists()" called on "java.io.File" object constructed from the absolute path to existing in the file system directory or file, whose name ends with space characters, for example "Directory1 ", "File1 ", "File2.txt ", always returns "false" value. The issue #2 is specific to Java Core Libraries area and independent from "javax.swing.JFileChooser" class. The separate bug JDK-8156657 was filed in order to address it.
11-05-2016

The bug was reproduced with JDK 9 b09, JDK 8u5 b13, JDK 7u55 b13 by means of the attached test case "FileChooserTrimsFileName.java".
24-04-2014

A minimal test case was created and attached to the bug record as the file "FileChooserTrimsFileName.java".
24-04-2014