【发布时间】:2014-05-05 06:03:41
【问题描述】:
我正在尝试在 SWT 中使用 DND(拖放)。 TreeView 似乎工作正常。我想将一个控件(截至目前的标签)从一个 TabItem 拖放到 TabFolder 中的另一个 TabItem。
这里我创建了一个 TabFolder 和两个 TabItems 并使用 setControl 来定义每个 TabItem 里面的内容
class DNDTab {
TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
tabFolder.setLayoutData(new GridData(SWT.FILL,GridData.FILL, true, true));
tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("Favorite");
tabItem.setControl(new CompositeFav(tabFolder));
tabItem = new TabItem(tabFolder, SWT.NONE);
tabItem.setText("Verified");
tabItem.setControl(new CompositeVerified(tabFolder));
}
这里定义了经过验证的 TabItem
class CompositeVerified extends Composite {
CompositeVerified(Composite parent) {
super(parent, SWT.NONE);
GridLayout layout = new GridLayout(1, false);
setLayout(layout);
String[] testList = {"My TestCase 1", "My TestCase 2",
"My TestCase 3", "My TestCase 4", "My TestCase 5"};
for (int i = 0; i < 5; i++) {
final Label dragLabel = new Label(this, SWT.NONE);
dragLabel.setText(testList[i]);
creatingDragSource(dragLabel);
/*** createDragSource is my defined function where each label within the TabItem
* is made as a drag source using "DragSource source = new DragSource(dragLabel, operations);"
***/
}
}
}
这里将Favorite TabItem作为放置目标
class CompositeFav extends Composite {
CompositeFav(final Composite parent) {
super(parent, SWT.NONE);
DropTarget target = new DropTarget(parent, operations);
target.setTransfer(types);
target.addDropListener(new DropTargetListener() {
/*** The part of the code where dragEnter, dragOver, drop events are added***/
}
}
}
拖动源创建良好,并且拖动似乎工作。但是当我将它放到 Fav TabItem 中时,它并没有被添加到那里。
【问题讨论】:
标签: java drag-and-drop swt