Search code examples
javaswtfiledialog

PathMustExists and FileMustExists in SWT FileDialog


To date I only used Swing to build graphical user interfaces but now I also want to make myself familiar with the Standard Widget Toolkit. I already read the documentation and built a simple app. My problem is now to use the FileDialog component.

I did the following code:

FileDialog openFileDialog = new FileDialog(shell, SWT.OPEN);
openFileDialog.setFilterExtensions(new String[] { "*.txt" });
openFileDialog.setFilterNames(new String[] { "Text files (*.txt)" });
openFileDialog.setText("Open file");
openFileDialog.open();

But I found no methods to set flags like "PathMustExists" or "FileMustExists". Is this not possible with FileDialog? Do I have to extend the class to implement that functionality? If so, how I have to proceed? Or this there a better OpenFileDialog component (maybe in JFace) from which I don't know?


Solution

  • There are no options for this.

    Since you are specifying SWT.OPEN you will get a file dialog specialized for opening existing files. Depending on which platform you are running on this dialog may not allow non-existent files to be selected at all (certainly true on Mac OS X). Still you should check the file after the dialog returns.