因为完整的从Java的组件到组件的代码在Eclipse的示例代码中非常详细就不重复了
因为我想实现的是windows Explorer到Java 程序的文件拖拽,所以,代码如下:
运行环境:JDK1.5
运行前把swt-win32-3236.dll文件import 到程序的Path 中
把
org.eclipse.swt_3.2.2.v3236b.jar
org.eclipse.swt.win32.win32.x86_3.2.2.v3236.jar
两个文件放到ClassPath中
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.DropTargetListener;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
public class DragTargetTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
final Table table = new Table (shell,SWT.BORDER);
table.setSize (200, 200);
for (int i = 0 ; i< 4 ; i++){
TableItem ti = new TableItem(table,SWT.NONE);
ti.setText("drop a file here ");
}
//上面是准备界面
int operations = DND.DROP_DEFAULT|DND.DROP_COPY;
DropTarget dropTarget = new DropTarget(table,operations);
dropTarget.setTransfer(new Transfer[] {FileTransfer.getInstance()});
dropTarget.addDropListener(new DropTargetListener(){
public void dragEnter(DropTargetEvent arg0) {
}
public void dragLeave(DropTargetEvent arg0) {
}
public void dragOperationChanged(DropTargetEvent arg0) {
//这里可以做拖放同时按Ctrl之类的控制键改变操作的处理
}
public void dragOver(DropTargetEvent arg0) {
arg0.detail = DND.DROP_COPY;
//这里比较重要,detail原值为DND.DROP_NONE改变detail才能让drop事件进行下去
}
public void drop(DropTargetEvent arg0) {
TableItem item = (TableItem) arg0.item;
if (item==null){
arg0.detail = DND.DROP_NONE;
return;
}
if (FileTransfer.getInstance().isSupportedType(arg0.currentDataType)){
String[] files = (String[])arg0.data;
if (files != null && files.length > 0){
item.setText(files[0]);//设置表格内容为文件名全路径
}
}
}
public void dropAccept(DropTargetEvent arg0) {
//最后一次取消drop操作的机会
System.out.println("dragAccept");
}
});
shell.setSize(400,400);
shell.open();
while (!shell.isDisposed()){
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
说明:
DropTarget 是可以单独使用的,不必和DragSource成对使用
代码只为说明问题,要想用到程序里面还需要做很多工作
参考资料:
<swt jface in action manning>
http://www.eclipse.org/articles/Article-SWT-DND/DND-in-SWT.html