我試圖將文件從Finder拖到我的SWT應用程序中。在Windows和Ubuntu,下面的代碼位的工作原理:在Mac上拖放不起作用
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();
}
}
我得到的印象是,我的應用程序沒有註冊自己是在Mac上放置目標,因爲徘徊文件在它不給我一滴光標。
我正在使用最新的SWT 3.5(我不能使用3.6,因爲爲了兼容性我需要堅持Carbon & Java 1.5)。
任何想法這裏有什麼問題嗎?
編輯:我修改了代碼,以便它是一個完全封閉的示例。它將丟棄的文件名打印到Windows和Ubuntu上的控制檯,但在Mac上不會執行任何操作。
您能否提供一個完全獨立的示例?提前致謝。 – Mot 2010-12-28 19:20:16
@mklhmnn我編輯了代碼。所有你需要做的是指定`import org.eclipse.swt。*`。還要注意我正在使用SWT 3.5。 – 2010-12-30 15:55:16