Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 850
Bluddymarri Avatar asked Aug 05 '13 12:08

Bluddymarri


Video Answer


1 Answers

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.

like image 124
Matthias Dornaus Avatar answered Sep 18 '22 17:09

Matthias Dornaus