Alexander Tumin
Alexander Tumin

Reputation: 645

SWT drag and drop custom Control displayed next to cursor

How do i display some custom Controls or widgets next to cursor when dragging in SWT?

Like Button or Tree or Table not Image.

If it is not possible - how do i render such a Button into Image?

Upvotes: 1

Views: 1490

Answers (2)

Alexander Tumin
Alexander Tumin

Reputation: 645

I've found the way of rendering arbitrary Control (widget) into Image using GC (canvas) class. Here is how:

dragSource.addDragListener(new DragSourceListener() {
    @Override
    public void dragStart(DragSourceEvent dragSourceEvent) {
        // getting control widget - Composite in this case
        Composite composite = (Composite) ((DragSource) dragSourceEvent.getSource()).getControl();
        // Getting dimensions of this widget
        Point compositeSize = composite.getSize();
        // creating new GC
        GC gc = new GC(composite);
        // Creating new Image
        Image image = new Image(Display.getCurrent(),compositeSize.x,compositeSize.y);
        // Rendering widget to image
        gc.copyArea(image,0,0);
        // Setting widget to DnD image
        dragSourceEvent.image = image;
    }
... other overriden methods ...
}

Upvotes: 2

cdc
cdc

Reputation: 2571

You could use an undecorated Shell that follows the mouse position, and in that Shell have the widget(s) you want to display.

Upvotes: 0

Related Questions