如何将控件从一个TabItem拖放到SWT中的另一个TabItem

时间:2014-05-05 06:03:42

标签: java drag-and-drop swt

我正在尝试在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);" 
             ***/
        }

    }
}

此处收藏的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时,它不会被添加到那里。

1 个答案:

答案 0 :(得分:0)

tabFolder首先捕获dropTargetEvent ... 丑陋的黑客:

  • 为tabFolder创建dropTarget
  • 将dropTargetEvent转发到目标复合

像这样:

DropTarget target = new DropTarget(tabFolder, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
        target.setTransfer(transfers);
        target.addDropListener(new DropTargetAdapter() {
            public void dragOver(DropTargetEvent event) {}

            public void dragEnter(DropTargetEvent event){}

            public void dragLeave(DropTargetEvent event){}

            public void drop(DropTargetEvent event) {
                DropTarget target = targetComposite.getData(DND.DROP_TARGET_KEY);
                target.getDropListeners()[0].drop(event);
            }
        });