SWT拖动到资源管理器(Windows)或Finder(OS X)

我有一个带有一堆图形元素的SWT应用程序。 我希望用户能够将元素拖到他们的桌面/ Windows资源管理器/ OS X Finder中。 当他们删除元素时,我需要将它们放到的路径,以便我可以在该位置创建一个表示该元素的文件。

我不认为我可以使用FileTransfer ,因为没有源文件。 有一个源对象可以创建一个文件,但只有一旦它知道放在哪里。

下面内联是我想要实现的一个简单示例,有一个带有标签的文本框可以拖动。 如果用户拖动到某个文件夹或文件,我想获取他们拖动的路径。 如果他们拖动到一个文件,我想用文本框中的任何内容替换该文件的内容。 如果他们拖动到一个文件夹,我想创建一个名为“TestFile”的文件,其中包含文本框中的内容。

 import org.eclipse.swt.SWT; import org.eclipse.swt.dnd.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class DesktopDragExample { public static void main(String[] args) { // put together the SWT main loop final Display display = Display.getDefault(); display.syncExec(new Runnable() { @Override public void run() { Shell shell = new Shell(display, SWT.SHELL_TRIM); initializeGui(shell); //open the shell shell.open(); //run the event loop while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } }); } // create the gui private static void initializeGui(Composite parent) { GridLayout layout = new GridLayout(2, false); parent.setLayout(layout); // make the instructions label Label infoLbl = new Label(parent, SWT.WRAP); GridData gd = new GridData(); gd.grabExcessHorizontalSpace = true; gd.horizontalAlignment = SWT.FILL; gd.horizontalSpan = 2; infoLbl.setLayoutData(gd); infoLbl.setText( "You should be able to drag to the desktop, Windows Explorer, or OS X Finder.\n" + "If you drag to a file, it will replace the contents of that file with the contents of the text box.\n" + "If you drag to a folder, it will create a file named 'TestFile' whose contents are whatever is in the text box."); // make the text element final Text text = new Text(parent, SWT.SINGLE | SWT.BORDER); gd = new GridData(); gd.grabExcessHorizontalSpace = true; gd.horizontalAlignment = SWT.FILL; text.setLayoutData(gd); // make the label element Label label = new Label(parent, SWT.NONE); label.setText("Drag me"); // listener for drags DragSourceListener dragListener = new DragSourceListener() { @Override public void dragStart(DragSourceEvent e) { e.detail = DND.DROP_COPY; } @Override public void dragFinished(DragSourceEvent e) { System.out.println("--dragFinished--"); System.out.println("e.data=" + e.data); } @Override public void dragSetData(DragSourceEvent e) { System.out.println("--dragSetData--"); System.out.println("e.data=" + e.data); } }; // the DragSource DragSource dragSource = new DragSource(label, DND.DROP_COPY); dragSource.setTransfer(new Transfer[]{FileTransfer.getInstance()}); dragSource.addDragListener(dragListener); } private static void draggedTo(String path, String textBoxContents) { System.out.println("Dragged the contents '" + textBoxContents + "' to '" + path + "'"); } } 

以下是其他一些有相同问题的人,但到目前为止看起来没有解决方案: 从SWT 拖到 桌面 , 将目标路径作为String 拖动

唯一的方法是创建一个临时文件,然后使用FileTransfer。 我怀疑这是你在本机代码中必须做的事情。 我会看看我是否有足够的时间来描绘样本……

您没有从中获取文件位置并自己编写文件。 拖动到桌面意味着FileTransfer(您可以检查dragSetData支持哪种类型的传输)。

这意味着SWT期望DragSourceEvent.data中的文件路径的String []。 如果在dragSetData方法中设置此项,则SWT会将这些文件复制到放置目标 – 例如桌面。

  @Override public void dragSetData(DragSourceEvent e) { System.out.println("--dragSetData--"); System.out.println("Is supported: " + FileTransfer.getInstance().isSupportedType(e.dataType)); FileTransfer f = FileTransfer.getInstance(); String[] filePaths = {"C:\\CamelOut\\4.xml" } ; e.data = filePaths; } };