Java Custom Drag and Drop – no callbacks to TransferHandler.
I want to implement Custom Drag and Drop functionality for a JPanel
subclass. I following the guidelines of the standard DnD Tutorial:
Drag and Drop and Data Transfer
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.
So, let’s review what I did…
I made my own Container class which declares itself to extend JPanel:
public class DnDUnitPanel extends JPanel
{
...
}
I copied the ListTransferHandler from this Demo:
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.
I then instantiate two different instances of DnDUnitPanel like this:
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());
(and I also create some JLabel instances and add() them to the panels
(not shown)).
When I try to drag a JLabel from one panel to another, nothing happens.
So I went back and reread this page:
in particular, what it says about setDragEnabled():
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.
JPanel does not have a setDragEnabled() method. So, I asked myself, what
does that really mean: “component that supports the drag gesture”?
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.
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.
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.)
Unfortunately, providing those (and uncommenting the calls above to
DnDUnitPanel.setDragEnabled()) also had no effect.
So… the TransferHandler never gets called(). Obviously, something
important is missing here, but I’m not seeing what that might be.
I’m at a dead end for what to try next.
Anybody see what is missing here?
Basically you need a data source.
Look at the other Stackoverflow question, here and here (a good explanation of DnD)