2010-12-22 85 views
11

我試圖將文件從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上不會執行任何操作。

+0

您能否提供一個完全獨立的示例?提前致謝。 – Mot 2010-12-28 19:20:16

+0

@mklhmnn我編輯了代碼。所有你需要做的是指定`import org.eclipse.swt。*`。還要注意我正在使用SWT 3.5。 – 2010-12-30 15:55:16

回答

0

這是SWT中的一個錯誤(issue #267381是相關的,但可能不是真正的問題)。

正如Mike L.在評論中指出的那樣,它在SWT 3.7M4中得到了修復。

2

既然你已經得到了線

dt.addDropListener(new DropTargetAdapter() {

它可能意味着錯誤的存在發展上的Mac OS X的Java SWT應用程序(可能固定在以後的版本發佈可能?)

做檢查了相關計算器問題措辭另一種方式@here

,它可能正是您的問題已提交作爲錯誤功能請求。