title: Java Custom Drag and Drop in JPanel
body: <p>Java Custom Drag and Drop - no callbacks to TransferHandler.</p>
  
  <p>I want to implement Custom Drag and Drop functionality for a JPanel
  subclass.  I following the guidelines of the standard DnD Tutorial:</p>
  
  <p><a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/index.html" rel="noreferrer">Drag and Drop and Data Transfer</a></p>
  
  <p>On the surface, things seem pretty strightforward, but when I actually
  try it, I get no indication that any DnD behaviour is happening. In
  fact, none of the methods in my TransferHandler are called.</p>
  
  <p>So, let's review what I did...</p>
  
  <p>I made my own Container class which declares itself to extend JPanel:</p>
  
  <pre><code>public class DnDUnitPanel extends JPanel
  {
      ...
  }
  </code></pre>
  
  <p>I copied the ListTransferHandler from this Demo:</p>
  
  <p><a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/dropmodedemo.html" rel="noreferrer">Drop Demo</a></p>
  
  <p>renamed the class as DndUnitTransferHandler, trimmed out code that is
  referring to JList objects, and installed System.out.println() statments
  on each of the 5 methods in there.</p>
  
  <p>I then instantiate two different instances of DnDUnitPanel like this:</p>
  
  <pre><code>DnDUnitPanel topPanel = new DnDUnitPanel(new GridLayout(0, 4, 6, 6), true);
  // topPanel.setDragEnabled(true);
  topPanel.setName("Top Panel");
  topPanel.setTransferHandler(new DnDUnitTransferHandler());
  
  DnDUnitPanel bottomPanel = new DnDUnitPanel(new GridLayout(0, 4, 6, 6), true);
  // bottomPanel.setDragEnabled(true);
  bottomPanel.setName("Bottom Panel");
  bottomPanel.setTransferHandler(new DnDUnitTransferHandler());
  </code></pre>
  
  <p>(and I also create some JLabel instances and add() them to the panels
  (not shown)).</p>
  
  <p>When I try to drag a JLabel from one panel to another, nothing happens.
  So I went back and reread this page:</p>
  
  <p><a href="http://docs.oracle.com/javase/tutorial/uiswing/dnd/transferhandler.html" rel="noreferrer">Transfer Handler</a></p>
  
  <p>in particular, what it says about setDragEnabled():</p>
  
  <pre><code>turns on drag support. (The default is false.) This method is
  defined on each component that supports the drag gesture; the link
  takes you to the documentation for JList.
  </code></pre>
  
  <p>JPanel does not have a setDragEnabled() method. So, I asked myself, what
  does that really mean: "component that supports the drag gesture"?</p>
  
  <p>I first figured this must mean something that is declared to implement
  MouseListener and/or MouseMotionListener.  I modified DnDUnitPanel to
  declare that it implements both and provided all the methods. Having
  done so, I could see that mousePressed(), mouseClicked(),
  mouseDragged(), etc. were getting called, but still nothing in the
  TransferHandler was getting called, and still no drag cursor indicating
  something was dragged or is ready to be dropped.</p>
  
  <p>I then looked at the source for JList itself and decided that
  "supports the drag gesture" really just means something that has a
  'dragEnabled' property with the associated getter and setter.</p>
  
  <p>So, I declared the property and provided the getter and setter on
  DnDUnitPanel by just copying code straight from JList itself (thinking
  maybe something I don't fully understand is calling
  isDragEnabled() and looking for a true value to initiate DnD behavior.)</p>
  
  <p>Unfortunately, providing those (and uncommenting the calls above to
  DnDUnitPanel.setDragEnabled()) also had no effect.</p>
  
  <p>So... the TransferHandler never gets called().  Obviously, something
  important is missing here, but I'm not seeing what that might be.</p>
  
  <p>I'm at a dead end for what to try next.</p>
  
  <p>Anybody see what is missing here?</p>