Search code examples
javauser-interfacedrag-and-dropswtjava-canvas

Drag and Drop changes in canvas only visible after minimization


public void dropAccept(final DropTargetEvent event)
{
if (TextTransfer.getInstance().isSupportedType(event.currentDataType))
{
final String d=(String)TextTransfer.getInstance().nativeToJava(event.CurrentDataType);
GC gc = new(text);
//text is the name assigned to the Canvas
text.addPaintListener(new PaintListener()
 {
 public void paintControl(PaintEvent e)
 {
  int x= event.x- shell.getBounds().x - text.getBounds().x;
  int y=event.y - shell.getBounds().y - text.getBounds().y;
  e.gc.drawString(d, x, y);
  }
  }); } }

This code snippet is part of a larger class that implements drag drop of text onto a canvas. The problem is that, the actual dropping of text is not seen on the canvas after I drop it but only after I minimize the shell and then maximize it again. Can anyone please tell me how I can make drop actions immediately visible by modifying this code?


Solution

  • You have not done anything to cause the control to be redrawn. Call

    text.redraw();
    

    to request that the control is redrawn (by calling the paint listener).

    Note: If you add paint listeners on every drop you are going to end up with lots of listeners registered.