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().getText();|
Following code sets the text field to get the dropped text.
Here is the code of DragAndDropExample.java
Output will be displayed as:
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: October 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles