More Tutorials| Bioinformatics| Open Source| Photoshop| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 .

 

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

                         

» View all related tutorials
Related Tags: c eclipse ide class button io swt get ip widgets order vi widget using cli border this id package rad

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.