TreeWrapper: An easy way to add Drag and Drop support to JTrees.

TreeWrapper presents an easy way to add Drag and Drop support to any JTree that uses a DefaultTreeModel (or derivative).

For updated information on TreeWrapper visit the TreeWrapper homepage.

This is a list of features and limitations of TreeWrapper:

Basic usage.

Using a TreeWrapper is very simple. You just need one line of code:

    JTree myJTree = ...;
    // Add a wrapper around myJTree: That's all, folks!
    TreeWrapper myWrapper = new TreeWrapper( myJTree );
    

Providing custom popup handlers

You can provide popup menus for specific nodes by implementing the CustomPopupHandler interface. Then just set your custom popup handler into the TreeWrapper:
  class MyCustomPopupHandler
    implements CustomPopupHandler
  {
    public JPopupMenu getMenuAt(JTree aJTree, TreeNode aTreeNode)
    {
      private JPopupMenu myLeafPopupMenu = ...;
      private JPopupMenu myFolderPopupMenu = ...;
      if ( aTreeNode.isLeaf() )
      {
        return myLeafPopupMenu;
      }
      else
      {
        return myFolderPopupMenu;
      }
    }
  }
  ...
  
  MyCustomPopupHandler myHandler = new MyCustomPopupHandler();
  JTree myTree = ...
  // Create a wrapper for myTree
  TreeWrapper myWrapper = new TreeWrapper( myTree );
  // Set the custom popup handler into the wrapper
  myWrapper.setCustomPopupHandler( myHandler );
  
    

Listening to tree-tree drag and drop events

All TreeTreeDnDListeners are informed about drag and drop operations when the user drags a node from a JTree into the same or another JTree (both of them wrapped with respective TreeWrappers, of course).

By implementing a custom TreeTreeDnDListener you can control which nodes can be dragged into which others, and are informed when the drop operation finally happens.

Listening to String-tree drag and drop events

All StringTreeDnDListeners are informed about drag and drop operations between String sources (text fields, for instance, or even the Internet Explorer URL bar) into specific nodes of a JTree. These listeners indicate which nodes accept Strings and which nodes don't. These listeners are also informed when the drop operation happens, and may handle the drop operation themselves (by throwing a DnDVetoException or just let the TreeWrapper to handle the operation itself.