1

I am currently working on an application and need to let the user to save a file. I need that when the user presses a button, the FileDialog to open at a specific location. I tried dialog.setFilterPath(), but it's not working all the time. I searched the internet and I found this bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=426849 and I am in the exactly same situation. I have tried the workaround suggested in comment 7 with "\\?\" prefix and it's working somehow. My question is: why is that working? What that prefix really does? I tried to search but I couldn't find anything that I fully understand.

Thank you and best regards

user0412
  • 33
  • 4
  • [This Microsoft document](https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats) refers to that as the 'DOS device path' format. SWT is just passing the path to the native WIndows dialog that it uses, so it is the native dialog that is doing something special with that. Note that this prefix won't work on macOS or Linux. – greg-449 Jul 04 '20 at 11:26
  • Yes, I agree and I have seen that information aswell. I am just curious, what is that "something special" that makes it work. – user0412 Jul 04 '20 at 12:04

1 Answers1

0

Try this

Preferences prefs = Preferences.userRoot().node(getClass().getName());
JFileChooser chooser = new JFileChooser(prefs.get(LAST_USED_FOLDER,
    new File(".").getAbsolutePath()));
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    // do something
    prefs.put(LAST_USED_FOLDER, chooser.getSelectedFile().getParent());
}