简体   繁体   中英

Set the initial directory in SWT FileDialog

I'm working on an Eclipse RCP project and need to let the user select some file. For convenience, based on some conditions, the initial directory of the file choosing dialog should be set prior to opening it.

As I'm bound to Eclipse RCP / SWT, I am working with the org.eclipse.swt.widgets.FileDialog .
The documentation of this FileDialog points out to use the setFilterPath(String string) -method which should do exactly what I need (see documentation ).

   FileDialog dialog = new FileDialog(shell, SWT.OPEN);
   dialog.setFilterExtensions(new String [] {"*.html"});
   dialog.setFilterPath("c:\\temp");
   String result = dialog.open();

Unfortunately it is not working, at least not "every time".

I have currently no installation to check on it, but I'm quite sure that the feature would work totally fine on a Windows 200/XP/Vista machine. I am working with a Windows 7 machine and I think I am suffering from the behaviour described here for lpstrInitialDir .

At least, this is exactly the behaviour I am facing: The path is good the first time I open the dialog, but the second time, the path is initially set to the last chosen path. This seems to be convenient in most cases, but it is not in mine.

Can this be right? If so, have I any chance on changing the behaviour according to my needs?

Thanks for any helping answer!

I ran into the same problem on Windows 10 and found a solution that seems to be working for me. A code snippet from the DirectoryDialog led to the right direction:

if (filterPath != null && filterPath.length() > 0) {
        String path = filterPath.replace('/', '\\');
        char[] buffer = new char[path.length() + 1];
        path.getChars(0, path.length(), buffer, 0);
        if (COM.SHCreateItemFromParsingName(buffer, 0, COM.IID_IShellItem, ppv) == OS.S_OK) {
            IShellItem psi = new IShellItem(ppv[0]);
            /*
             * SetDefaultDirectory does not work if the dialog has
             * persisted recently used folder. The fix is to clear the
             * persisted data.
             */
            fileDialog.ClearClientData();
            fileDialog.SetDefaultFolder(psi);
            psi.Release();
        }
}

The FileDialog misses this statement 'fileDialog.ClearClientData()'. My solution is to execute the following code before setting the path and open the dialog:

long [] ppv = new long [1];
if (COM.CoCreateInstance(COM.CLSID_FileOpenDialog, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IFileOpenDialog, ppv) == OS.S_OK) {
    IFileDialog fileDialog = new IFileDialog(ppv[0]);
    fileDialog.ClearClientData();
    fileDialog.Release();
}

Now you can set the filterpath without Windows messing things up.

I found a simple Solution for the Problem you described (I had the exact same Problem).

Just rearrange the your code like this:

   FileDialog dialog = new FileDialog(shell, SWT.OPEN);
   dialog.setFilterPath("c:\\temp"); // This line is switched with the following line
   dialog.setFilterExtensions(new String [] {"*.html"});
   String result = dialog.open();

Somehow the Order of the methods called is relevant.

Are you using the same FileDialog object when you re-open it?

I ran a few quick tests and found that, if you re-set the filterPath, the dialog opens in the correct location.

If I open the same object again, it starts in the previously selected location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM