Drag’n’drop in SWT components

Those who went to my Lotusphere session already know this but doing drag’n’drop in SWT is actually quite easy. All you need to do is define a drag source and/or a drop target. The former is only required if a control in users should be able to drag data from a control in your code. The latter is required if you would like users to be able to drop stuff onto your component.

When setting up a drag source or a drop target you specify the control the source/target controls and the types of data you can provide/accept. These types are called Transfer types and are subclasses of the org.eclipse.ui.dnd.Transfer class.

Transfer types that might come handy – the class names should be pretty self-explanatory:

  • org.eclipse.swt.dnd.URLTransfer.getInstance()
  • org.eclipse.swt.dnd.FileTransfer.getInstance()
  • org.eclipse.swt.dnd.TextTransfer.getInstance()

Making a JFace viewer a drop target is simple passing in the underlying control:

DropTarget dropTarget = new DropTarget(viewer.getControl(),

Once the drop target has been created you set the transfer types:

final Transfer[] dropTransferTypes = new Transfer[] {

The last thing you need is to add an instance of DropTargetListener or use a subclass of DropTargetAdapter as I do here:

dropTarget.addDropListener(new DropTargetAdapter() {

Creating a drag source is analogous.

Dragging Sametime buddies around

I have been playing a little with the drag’n’drop support in the Notes 8.5 beta client and found out that a VCard is created the you drag a Sametime contact to a place that accepts dragging files to (this probably also works in earlier versions). This means that dragging a Sametime buddy to the desktop will create a VCard for that person. Nice! Dragging the buddy to an e-mail (or other place which accepts text transfer) will create a mailto-link.

Just one of those nice, time saving features…