title: How do I implement intelligent drag and drop from one JTree to another?
body: <p>I have two JTrees with some mock data in there, what I'm looking to do is to be able to take each 'Job' (15663-1, 15663-2, etc) and make a node for each, with a node for each part underneath it and the components attached to each part underneath that.  In two trees, like this: </p>
  
  <pre><code>+------------------------------+------------------------------+
  | PARTS TO BE SHIPPED          | SHIPPING BOX                 |
  +------------------------------+------------------------------+
  |[JOB]                         |[JOB]                         |
  |+------[part]                 |+------[part]                | 
  |        +------[component]    |        +------[component]    |
  |        +------[component]    |        +------[component]    |
  |+------[part]                 |+------[part]                 |
  |        +------[component]    |        +------[component]    |
  |[JOB]                         |[JOB]                         |
  |+------[part]                 |+------[part]                 |
  |        +------[component]    |        +------[component]    |
  |        +------[component]    |        +------[component]    |
  |+------[part]                 |+------[part]                 |
  |        +------[component]    |        +------[component]    |
  +------------------------------+------------------------------+
  </code></pre>
  
  <p>So that supposing I have two screws in the cover in job A in the 'parts to be shipped' jtree and I don't have anything in jobA in the shipping box, when I drag the screws over to the shipping box it should make an entry for jobA, make an entry for part A and make an entry for the component, then I want it to prompt for the quantity for that component and subtract that quantity from the parts to be shipped jtree. </p>
  
  <p>So if I have a job called 1553-4 and it has a cover with four screws and I drag the screws into the shipping box then it should make an entry in the shipping box that says, "x screws" then prompt for the user to enter the number of screws they just packaged, if they package two screws then the jtree should change to reflect the 2 screws remaining for that job.</p>
  
  <p>I have read a bunch of different drag and drop tutorials and I have some examples but I just cannot seem to get it. Any advice or help would be appreciated.</p>
  
  <p>I know that I need to implement a TranferHandler but I'm not sure exactly how, there seems to be too much interface 'magic' going on and I'm really not understanding it.</p>
  
  <p>This is what I have, I understand making nodes and such, here is what I have:</p>
  
  <pre><code>package com.protocase.examples;
  
  
  import java.awt.Dimension;
  import java.awt.HeadlessException;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTree;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.DefaultTreeModel;
  import javax.swing.tree.MutableTreeNode;
  
  /**
   * @author DavidH
   */
  public class JTreeExample {
      public static void main(String[] args) {
          addTreesAndDisplay();
  
      }
  
      private static void addTreesAndDisplay() throws HeadlessException {
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
  
  
          JTree tree = new JTree(getTreeModel());
          tree.setDragEnabled(true);
          tree.setPreferredSize(new Dimension(200,400));
          JScrollPane scroll = new JScrollPane();
          scroll.setViewportView(tree);
          panel.add(scroll);
  
  
          JTree secondTree = new JTree(getTreeModel());
          secondTree.setPreferredSize(new Dimension(200,400));
          secondTree.setDragEnabled(true);
          JScrollPane secondScroll = new JScrollPane();
          secondScroll.setViewportView(secondTree);
          panel.add(secondScroll);
  
  
          frame.setContentPane(panel);
          frame.pack();
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
  
      private static DefaultTreeModel getTreeModel() {
          MutableTreeNode root =  new DefaultMutableTreeNode("15663-1");
          DefaultMutableTreeNode cover = new DefaultMutableTreeNode("Cover");
          DefaultMutableTreeNode base = new DefaultMutableTreeNode("Base");
          root.insert(cover, 0);
          root.insert(base, 0);
          cover.insert(new DefaultMutableTreeNode("2x PEMS"), 0);
          cover.insert(new DefaultMutableTreeNode("2x SCREWS"), 0);
          base.insert(new DefaultMutableTreeNode("4x SCREWS"), 0);
          base.insert(new DefaultMutableTreeNode("4x HANDLES"), 0);
          DefaultTreeModel model = new DefaultTreeModel(root);
          return model;
      }
  }
  </code></pre>
  
  <p>I'm just looking for a concise drag and drop example of dragging into a JTree and dragging from a JTree.</p>