Drag and drop of a group of files into a tree : JTree « Swing « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing » JTree 
14. 66. 34. Drag and drop of a group of files into a tree
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.TransferHandler;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class MyTree extends JTree {

  public MyTree() {
    super();
    setDragEnabled(false);
    setTransferHandler(new FSTransfer());
  }

}

class FSTransfer extends TransferHandler {
  public boolean importData(JComponent comp, Transferable t) {
    if (!(comp instanceof MyTree)) {
      return false;
    }
    if (!t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
      return false;
    }
    MyTree tree = (MyTreecomp;
    DefaultTreeModel model = (DefaultTreeModeltree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNodemodel.getRoot();
    try {
      List data = (Listt.getTransferData(DataFlavor.javaFileListFlavor);
      Iterator i = data.iterator();
      while (i.hasNext()) {
        File f = (Filei.next();
        root.add(new DefaultMutableTreeNode(f.getName()));
      }
      model.reload();
      return true;
    catch (Exception ioe) {
      System.out.println(ioe);
    }
    return false;
  }

  public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
    if (comp instanceof MyTree) {
      for (int i = 0; i < transferFlavors.length; i++) {
        if (!transferFlavors[i].equals(DataFlavor.javaFileListFlavor)) {
          return false;
        }
      }
      return true;
    }
    return false;
  }
}
14. 66. JTree
14. 66. 1. Creating a JTree
14. 66. 2. Using Vector to store JTree valuesUsing Vector to store JTree values
14. 66. 3. Create JTree from Vector
14. 66. 4. Working with the Nodes of the TreeWorking with the Nodes of the Tree
14. 66. 5. Scrolling Trees: public void scrollPathToVisible(TreePath path) and public void scrollRowToVisible(int row)Scrolling Trees: public void scrollPathToVisible(TreePath path) and public void scrollRowToVisible(int row)
14. 66. 6. Traversing TreesTraversing Trees
14. 66. 7. JTree.lineStyle client propertyJTree.lineStyle client property
14. 66. 8. Changing the icons of a tree: UIManager.put(Tree.openIcon, new ImageIcon(yourFile.gif));Changing the icons of a tree: UIManager.put(Tree.openIcon, new ImageIcon(yourFile.gif));
14. 66. 9. Produce horizontal lines between level-one nodesProduce horizontal lines between level-one nodes
14. 66. 10. Using Boolean value cell renderer to display boolean and non-boolean valuesUsing Boolean value cell renderer to display boolean and non-boolean values
14. 66. 11. Creating DefaultTreeCellEditor from its Default Tree RendererCreating DefaultTreeCellEditor from its Default Tree Renderer
14. 66. 12. Adding children DefaultMutableTreeNode to its parentAdding children DefaultMutableTreeNode to its parent
14. 66. 13. JTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION)
14. 66. 14. Traveling to all the nodesTraveling to all the nodes
14. 66. 15. JTree.DynamicUtilTreeNode ClassJTree.DynamicUtilTreeNode Class
14. 66. 16. Tree Will Expand ListenerTree Will Expand Listener
14. 66. 17. To find out when a tree node has been expanded or collapsedTo find out when a tree node has been expanded or collapsed
14. 66. 18. Dynamic Tree
14. 66. 19. Have the row height for each row computed individually
14. 66. 20. Setting the Row Height of a JTree
14. 66. 21. All rows will be given 15 pixels of height
14. 66. 22. Preventing Expansion or Collapse of a Node in a JTree: override JTree.setExpandedState()
14. 66. 23. Listening for Expansion and Collapse Events in a JTree Component
14. 66. 24. Expansion and Collapse Events in a JTree are fired before a node is expanded or collapsed can be vetoed, thereby preventing the operation.
14. 66. 25. Creating a JTree Component
14. 66. 26. Visiting All the Nodes in a JTree Component
14. 66. 27. Traverse all expanded nodes in tree
14. 66. 28. Finding a Node in a JTree Component
14. 66. 29. Search backward from last visible row looking for any visible node whose name starts with prefix.
14. 66. 30. Find the path regardless of visibility that matches the specified sequence of names
14. 66. 31. Preventing the Expansion or Collapse of a Node in a JTree Component
14. 66. 32. Have a popup attached to a JTree
14. 66. 33. Adding editable nodes to JTree
14. 66. 34. Drag and drop of a group of files into a tree
14. 66. 35. JTree Look and Feel
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.