Drag & drop not working on Mac
I'm trying to make it possible to drag files from the Finder into my SWT application. On Windows and Ubuntu, the following bit of code works:
public class DndTest {
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display, SWT.DIALOG_TRIM);
shell.setText("Drag & drop test");
shell.setSize(200, 200);
final FormLayout layout = new FormLayout();
shell.setLayout(layout);
final Label lbl = new Label(shell, SWT.NORMAL);
lbl.setAlignment(SWT.CENTER);
lbl.setText("Drop files here");
final FormData layoutData = new FormData();
layoutData.left = new FormAttachment(50, -100);
layoutData.top = new FormAttachment(50, -15);
layoutData.right = new FormAttachment(50, 100);
layoutData.bottom = new FormAttachment(50, 15);
lbl.setLayoutData(layoutData);
final DropTarget dt = new DropTarget(shell,
DND.DROP_DEFAULT | DND.DROP_MOVE);
final FileTransfer fileTransfer = FileTransfer.getInstance();
dt.setTransfer(new Transfer[] { fileTransfer });
dt.addDropListener(new DropTargetAdapter() {
@Override
public void drop(final DropTargetEvent event) {
System.out.println(event);
String fileList[] = null;
final FileTransfer ft = FileTransfer.getInstance();
if (ft.isSupportedType(event.currentDataType)) {
fileList = (String[]) event.data;
}
for (final String file : fileList) {
System.out.println("- " + file);
}
}
});
shell.open();
whil开发者_Python百科e (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
I get the impression that my application is not registering itself as a drop target on Mac, because hovering files over it does not give me a drop cursor.
I'm using the latest SWT 3.5 (I cannot use 3.6 because for compatibility I need to stick with Carbon & Java 1.5).
Any idea what's wrong here?
Edit: I revised the code so that it's a fully enclosed example. It prints the dropped filenames to the console on Windows and Ubuntu, but does nothing on Mac.
Since you've got the line
dt.addDropListener(new DropTargetAdapter() {
it could possibly mean the existence of a bug for developing Java SWT applications on Mac OS X (maybe fixed in a later released version possibly?)
do check out the related stackoverflow question phrased another way @ here
and it might be exactly your problem already submitted as a bug feature request.
This was a bug in SWT (issue #267381 is related, but might not be the actual issue).
As Mike L. pointed out in a comment, it was fixed in SWT 3.7M4.
精彩评论