SWT 的文字拖动实现 DragSource 和DropTarget 实现问题 DragSource 和DropTarget 可以实现控件的拖放,但是当把StyledText作为DragSource 对象时,发现StyledText不能用鼠标选择部分文字,但是可以用shift+方向键或者双击StyledText获取部分选择,请教各位高手是什么原因?能否实现StyledText中的选中文字拖动? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 老兄 转贴一段你看看 或许有用 final StyledText text1 = new StyledText(shell, style); text1.setText(string1); DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE); source.setTransfer(new Transfer[] {TextTransfer.getInstance()}); source.addDragListener(new DragSourceAdapter() { Point selection; public void dragStart(DragSourceEvent e) { selection = text1.getSelection(); e.doit = selection.x != selection.y; } public void dragSetData(DragSourceEvent e) { e.data = text1.getText(selection.x, selection.y-1); } public void dragFinished(DragSourceEvent e) { if (e.detail == DND.DROP_MOVE) { text1.replaceTextRange(selection.x, selection.y - selection.x, ""); } selection = null; } }); final StyledText text2 = new StyledText(shell, style); text2.setText(string2); DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK); target.setTransfer(new Transfer[] {TextTransfer.getInstance()}); target.addDropListener(new DropTargetAdapter() { public void dragEnter(DropTargetEvent e) { if (e.detail == DND.DROP_DEFAULT) e.detail = DND.DROP_COPY; } public void dragOperationChanged(DropTargetEvent e) { if (e.detail == DND.DROP_DEFAULT) e.detail = DND.DROP_COPY; } public void drop(DropTargetEvent e) { text2.insert((String)e.data); } }); 非常感谢moneybox,我已经测试了这段代码,非常符合要求!我前一段自己也作了一部分就是给styledText加了一个drag的监听,但是在上面加的判断很多而且效果勉强可以,很不稳定!有了你这段代码效果非常好,再次感谢!同时也谢谢lbfhappy的回复,不过整体已经用了SWT,再用swing就不是太好了。 java生成二维码图片清晰度问题 java 发 email 附件问题 不知道是什么错误,怎么改呢? 异常出现后,虚拟机会如何处理呢? 请问怎么让程序每阁一段时间运行一个指令啊? 求如何判断五指棋胜负的算法 Java高手请进来!!! 一个package的问题,小妹只有30分了,马上结贴 如何获取当前目录(或当前class的所在目录)? 请教高手一个Java Socket的入门级的小问题。 构造器(构造函数)的作用!!!!!! yyyyMMdd格式的日期字符串比较
final StyledText text1 = new StyledText(shell, style);
text1.setText(string1);
DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
source.setTransfer(new Transfer[] {TextTransfer.getInstance()});
source.addDragListener(new DragSourceAdapter() {
Point selection;
public void dragStart(DragSourceEvent e) {
selection = text1.getSelection();
e.doit = selection.x != selection.y;
}
public void dragSetData(DragSourceEvent e) {
e.data = text1.getText(selection.x, selection.y-1);
}
public void dragFinished(DragSourceEvent e) {
if (e.detail == DND.DROP_MOVE) {
text1.replaceTextRange(selection.x, selection.y - selection.x, "");
}
selection = null;
}
});
final StyledText text2 = new StyledText(shell, style);
text2.setText(string2);
DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK);
target.setTransfer(new Transfer[] {TextTransfer.getInstance()});
target.addDropListener(new DropTargetAdapter() {
public void dragEnter(DropTargetEvent e) {
if (e.detail == DND.DROP_DEFAULT)
e.detail = DND.DROP_COPY;
}
public void dragOperationChanged(DropTargetEvent e) {
if (e.detail == DND.DROP_DEFAULT)
e.detail = DND.DROP_COPY;
}
public void drop(DropTargetEvent e) {
text2.insert((String)e.data);
}
});