java拖拽⽂件和windows交互使⽤⿏标钩⼦⽐较⽅便
1:在拖动开始的时候打开钩⼦
2:⿏标钩⼦监听⿏标松开的时候,传⽂件路径
3:拖动完成之后关闭⿏标钩⼦
缺点: 不能跨平台
前提:
org.sf.feeling.sion_1.1.0.jar(需要下载)
DragSource datasource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY); // tree: lipse.swt.widgets.Tree
datasource.setTransfer(new Transfer[] { Instance()});
datasource.addDragListener(new DragSourceListener() {
private HookListener hookListener;
private String[] paths;
public void dragStart(DragSourceEvent event) {
TreeItem selection = Selection()[0];
if (Text().length() == 0) {
event.doit = false;
}
Hook.MOUSE.install("File");
hookListener = new HookListener();
Hook.MOUSE.addListener("File", hookListener);
}
public void dragSetData(DragSourceEvent event) {
if (HookFlag() == HookListener.L_RELEASE) {
event.doit = true;
if (paths == null || paths.length == 0) {
paths = getSelectPaths();
}
event.data = paths;
} else {
event.doit = false;
}
}
eclipse怎么打开已有的java文件public void dragFinished(DragSourceEvent event) {
veListener("File", hookListener);
Hook.MOUSE.uninstall("File");
paths = null;
}
}
// 内部类
class HookListener implements HookEventListener {
private int hookFlag = -1;
private final static int L_RELEASE = 10;
public void acceptHookData(HookData hookData) {
if (hookData != null) {
}
if (WParam() == 514) { // ⿏标松开时候触发 hookFlag = L_RELEASE;
}
}
public int getHookFlag() {
return hookFlag;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论