9

I need to use

fileChooser.getSelectedFile()

method however it always returns language modified path because some directories are translated in osX. For example folder "/Downloads" is translated to my system language "/Stiahnuté" but real path is "/Downloads"

return:

/Users/John/Stiahnuté

expectation

/Users/John/Downloads

If I select some sub-directory then fileChooser.getSelectedFile() returns right path again. It looks that always only last directory in path is translated

/Users/John/Downloads/subDirectory

Code:

saveButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setFileFilter(new FolderFilter());
                fileChooser
                        .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    System.out.println("save path: "
                            + selectedFile.getPath());
                    doSomething(selectedFile);
                }
            }
       });

UPDATE:

I made little workaround but it is not perfect solution. However it works for me.

JFileChooser fileChooser = new JFileChooser();
                FileNameExtensionFilter filter = new FileNameExtensionFilter(
                        "Directories", "dir");
                fileChooser.setFileFilter(filter);
                if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    File newDir = new File(selectedFile.getPath());
                    if (!newDir.exists()) {
                        newDir.mkdir();
                    }
                    doSomething();
                }
Matwosk
  • 459
  • 1
  • 9
  • 25
  • 1
    I would try `fileChooser.getSelectedFile().getCanonicalFile()`. If that doesn't work, try using the more modern Path class: `fileChooser.getSelectedFile().toPath().toRealPath().toFile()` – VGR May 03 '16 at 00:05
  • unfortunately java.nio.file.NoSuchFileException: /Users/John/Stiahnuté – Matwosk May 03 '16 at 00:32

1 Answers1

1

I can reproduce the problem on Mac OS X 10.11.4 with Java 1.8.0_66. For me this looks like a bug (or at least unexpected behavior) in the implementation of JFileChooser. You may open a bug report for the issue.

With the help of an answer explaining to use FileDialog to get a operating system native file chooser and another answer about using it to select directories I found the following workaround:

final Frame parent = …; // can be null

System.setProperty("apple.awt.fileDialogForDirectories", "true");
final FileDialog fileDialog = new FileDialog(parent);
fileDialog.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");

final File selectedDirectory = new File(fileDialog.getDirectory(), fileDialog.getFile());
System.out.println(selectedDirectory);
System.out.println(selectedDirectory.exists());

Note that using "apple.awt.fileDialogForDirectories" is, of course, platform specific and will not work on other operating systems.

Community
  • 1
  • 1
siegi
  • 5,646
  • 2
  • 30
  • 42