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();
}
Simply put, getPath() returns the String representation of the file's abstract pathname. This is essentially the pathname passed to the File constructor. So, if the File object was created using a relative path, the returned value from getPath() method would also be a relative path.
getPath(): The getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object. getAbsolutePath(): The getAbsolutePath() returns a path object representing the absolute path of given path.
getAbsolutePath() method returns the absolute pathname string of this abstract pathname.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With