SWT File Dialog

During the development of an Rich Client Platform (RCP) it is common to develop a file browse dialog, here’s how to show a file dialog in SWT:

import org.eclipse.swt.widgets.FileDialog;

FileDialog dialog = new FileDialog(this.getShell(), SWT.NULL);
dialog.setFilterExtensions(new String[] { "*.txt", "*.*" });
dialog.setFilterNames(new String[] { "Text files", "All files" });
String path = dialog.open();
if (path != null) {
   File file = new File(path);
   if (file.isFile()) {

The snippet above filters by text files (*.txt) and all files (*.*) and it is easily applied to the click event of any button or file menu option.