Drag & drop not working on Mac

爱⌒轻易说出口 提交于 2019-12-05 15:58:17

问题


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();

        while (!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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/4507559/drag-drop-not-working-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!