Home Tutorials Swt Drag and Drop Example in SWT
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Drag and Drop Example in SWT

Advertisement
Drag and Drop in Java - This section is going to illustrates you how to create a program to drag and drop the tree item in Java .

Drag and Drop Example in SWT

     

Drag and Drop in Java - This section is going to illustrates you how to create a program to drag and drop the tree item in Java .

In SWT, the class DND provides all the constants to drag the source and drop. The class DragSource defines the source for drag transfer. The method setTransfer() sets the text to be transferred by the DragSource. The class TextTransfer converts the plain text into a specific representation of data. The method getInstance() returns the instance of TextTransfer class. The method addDragListener() notifies when a drag and drop operation is in progress by calling the class DragSourceAdapter. 

The class Tree allows to create the hierarchy of items. We have create a tree to drag the tree item and drop it into the text. The method getSelection() returns an array of selected tree items.

The class DropTarget defines the target to drop the text. The method setTransfer() specifies the text that can be transferred to the DropTarget. The method addDropListener() notifies when a drag and drop operation is in progress by calling the class DropTargetAdapter. 

Following code sets the data to be the first selected item's text:

event.data = tree.getSelection()[0].getText();

Following code sets the text field to get the dropped text.

text.setText((String) event.data);

Here is the code of DragAndDropExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class DragAndDropExample {
  public static void main(String[] args) {

  Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setLayout(new FillLayout());
  shell.setText("Drag and Drop");
  final Tree tree = new Tree(shell, SWT.BORDER);
  TreeItem item1 = new TreeItem(tree, SWT.NONE);
  item1.setText("Hello 1");
  TreeItem item2 = new TreeItem(tree, SWT.NONE);
  item2.setText("Hello 2");
  TreeItem item3 = new TreeItem(tree, SWT.NONE);
  item3.setText("Hello 3");
  TreeItem item4 = new TreeItem(tree, SWT.NONE);
  item4.setText("Hello 4");

  DragSource ds = new DragSource(tree, DND.DROP_MOVE);
  ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });
  ds.addDragListener(new DragSourceAdapter() {
  public void dragSetData(DragSourceEvent event) {
  event.data = tree.getSelection()[0].getText();
  }
  });
  final Text text = new Text(shell, SWT.BORDER);
  DropTarget dt = new DropTarget(text, DND.DROP_MOVE);
  
  dt.setTransfer(new Transfer[] { TextTransfer.getInstance() });
  dt.addDropListener(new DropTargetAdapter() {
  public void drop(DropTargetEvent event) {
  text.setText((String) event.data);
  }
  });
  shell.setSize(250100);
  shell.open();
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  display.dispose();
  }
}

Output will be displayed as:

Download Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 3, 2008

Related Tutorials

Ask Questions?    Discuss: Drag and Drop Example in SWT   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sudha
July 30, 2012
ip

something something
DMCA.com