Alexander Tumin Alexander Tumin - 4 years ago 122
Java Question

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?

Answer Source

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

dragSource.addDragListener(new DragSourceListener() {
    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
        // Setting widget to DnD image
        dragSourceEvent.image = image;
... other overriden methods ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download