2

I am trying to open a SWT-file-browser in on button click event.

tnAddFiles.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                     new FileBrowser();

                }
            });

I am getting this error

Exception in thread "main" org.eclipse.swt.SWTError: Not implemented [multiple displays]
    at org.eclipse.swt.SWT.error(SWT.java:4387)
    at org.eclipse.swt.widgets.Display.checkDisplay(Display.java:706)
    at org.eclipse.swt.widgets.Display.create(Display.java:807)
    at org.eclipse.swt.graphics.Device.<init>(Device.java:130)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:699)
    at org.eclipse.swt.widgets.Display.<init>(Display.java:690)
    at com.rawzor.ui.FileBrowser.<init>(FileBrowser.java:34)
Baz
  • 36,440
  • 11
  • 68
  • 94
HDdeveloper
  • 4,396
  • 6
  • 40
  • 65

1 Answers1

7

I guess you are using the implementation from that website. If this is the case, this code uses the following line:

Display display = new Display();

which will initialize a new Display. Since your application already has a Display this will lead to the

SWTError: Not implemented [multiple displays]

The solution would be to either use the built-in classes FileDialog for file selection:

FileDialog fd = new FileDialog(shell, SWT.OPEN);
fd.setText("Open file");
fd.setFilterPath("C:/");
String[] filterExt = { "*.txt", "*.doc", ".rtf", "*.*" };
fd.setFilterExtensions(filterExt);
String file= fd.open();
System.out.println(file);

or DirectoryDialog for directory selection:

DirectoryDialog dlg = new DirectoryDialog(shell);
dlg.setText("Choose directory");
dlg.setFilterPath("C:/");
String dir = dlg.open();
System.out.println(dir);
Baz
  • 36,440
  • 11
  • 68
  • 94