Simple DND (Drag and Drop) Example : Drag Drop « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » SWT JFace Eclipse » Drag DropScreenshots 
Simple DND (Drag and Drop) Example
Simple DND (Drag and Drop) Example


import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class SimpleDNDExample {

  public static void main(String[] args) {
    Shell shell = new Shell();
    shell.setLayout(new FillLayout());

    // Create the tree and some tree items
    final Tree tree = new Tree(shell, SWT.NONE);
    TreeItem item1 = new TreeItem(tree, SWT.NONE);
    item1.setText("Item 1");
    TreeItem item2 = new TreeItem(tree, SWT.NONE);
    item2.setText("Item 2");
    TreeItem item3 = new TreeItem(tree, SWT.NONE);
    item3.setText("Item 3");
    TreeItem item4 = new TreeItem(tree, SWT.NONE);
    item4.setText("Item 4");

    // Create the drag source on the tree
    DragSource ds = new DragSource(tree, DND.DROP_MOVE);
    ds.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    ds.addDragListener(new DragSourceAdapter() {
      public void dragSetData(DragSourceEvent event) {
        // Set the data to be the first selected item's text
        event.data = tree.getSelection()[0].getText();
      }
    });

    // Create the button
    final Button button = new Button(shell, SWT.FLAT);
    button.setText("Button");
    button.setAlignment(SWT.CENTER);

    // Create the drop target on the button
    DropTarget dt = new DropTarget(button, DND.DROP_MOVE);
    dt.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    dt.addDropListener(new DropTargetAdapter() {
      public void drop(DropTargetEvent event) {
        // Set the buttons text to be the text being dropped
        button.setText((Stringevent.data);
      }
    });

    shell.pack();
    shell.open();
    Display display = Display.getDefault();
    while (!shell.isDisposed())
      if (!display.readAndDispatch())
        display.sleep();
    display.dispose();
  }

}



           
       
Related examples in the same category
1. SWT DND (Drag and Drop) comprehensive Example SWT DND (Drag and Drop) comprehensive Example
2. Word JumblesWord Jumbles
3. Illustrates draggingIllustrates dragging
4. SWT DnD (Drag and drop) Composite
5. Drag and Drop: determine native data types available (motif only)
6. Drag and Drop: determine data types available (win32 only)Drag and Drop: determine data types available (win32 only)
7. Drag and Drop example snippet: define a default operation (in this example, Copy)Drag and Drop example snippet: define a default operation (in this example, Copy)
8. Drag and Drop example snippet: define my own data transfer typeDrag and Drop example snippet: define my own data transfer type
9. Drag and Drop example snippet: drag leaf items in a treeDrag and Drop example snippet: drag leaf items in a tree
10. Drag and Drop example snippet: drag text between two labelsDrag and Drop example snippet: drag text between two labels
w_w___w___.j___a_v_a___2s__.__com_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.