It seems JTree DnD support is a quit difficult feature to implement of all swing components. I'm writing an application in which I have a JTree structure representing the file system of user machine and another JTable component at the right.
I want to be able to drag files nodes from left JTree to right JTable.
I would appreciate a lot if someone share with me some source code examples for this functionality. can someone post some basic java code to get me started or point me to some web resource discussing this feature?
I haven't looked closely at Mr. Wood's code, but if you can presume jdk 1.4 or better, I think it would probably be easier to use the TransferHandler class.
JTree myTree = new JTree(...); JTable myTable = new JTable(...); myTree.setTransferHandler(...); // pass a handler for the drag-out myTable.setTransferHandler(...); // pass a handler for the drop-into
It's that easy. Ok, not really because you have to actually write the TransferHandlers, but it's not so bad. It's easier to comprehend than the DragGesture/DropTarget stuff, and Sun does provide some examples.
a few notes, because I'm oh so helpful:
1) If you can presume 1.6 or better, you probably want to call myTable.setDropMode() for better drop feedback.
2) If it might be run on 1.5, you probably want to set the sun.swing.enableImprovedDragGesture system property. [edit: I've had trouble with this on Macs, in that it made dropping impossible. So either set sun.swing.enableImprovedDragGesture only on non-Macs, or test well.]
3) If you want to convey Java objects within the same JVM and you don't want your objects to be silently serialized and deserialized, set the MIME type of your Transferable to something like DataFlavor.javaJVMLocalObjectMimeType+";class=the.fully.qualified.ClassName". The Sun tutorial linked above mentions this, but it's easy to miss. [ December 14, 2006: Message edited by: Brian Cole ]
the code posted by Mr.craig is excellent and works. but when i tried integrating it into my complex TreeTable component i was frustrated by a DnD problem on Linux platform.the code works on win & Mac but fails on Linux with an error: cannot find top level for source drag component.
Hi guys, I'm still having problems with JTree DnD ... I wrote a drag listener class for my JTree and the code runs well on windows.
however when i tested on Linux i got this exception:
any Ideas where the problem is ?
thanks for helping.
Joined: Jan 14, 2004
I don't have access to linux but I did a little snooping around for you. Instead of creating a new DragSource you could try DragSource.getDefaultDragSource to see if it gives a more useful platform–specific reference. The next–to–last paragraph in the comments section of the DragSource api offers two possibilities for this exception. Some possibly relevant JDK Bug research results: For drag&drop: JTree nodes which implement Drag&Drop and Mouse Event act incorrect Dnd with JTrees does not work on linux/motif For dragndrop: JWindow on Linux can't accept a drop XDnD support The Evaluation section of the first bug report in the list above suggests that the AWT drag_n_drop classes do not work so well with Swing components, especially with respect to drag gesture recognition. Brian Cole's recommendation to use TransferHandlers may be the better way to go.
Thank you Mr Craig for pointing out these DnD bugs.
I actually wrote a TransferHandler and it seems it worked well on Linux. but sometime I'm having some weired behavior of the JTree : when i select a Node & start dragging ; the JTree node disappear or change position & goes up ..brief the Jtree nodes get messed up..I'm guessing it's an issue with mouse listener , or may be TreeSelectionListener or smth else..I don't know where the problem is..here is my TransferHandler:
can you please help me write a working & cross platform TransferHandler of my JTree ? that would be very nice.
Joined: Jan 14, 2004
sometime I'm having some weired behavior of the JTree This is likely because of the added DragHandler conflicting with the default drag_and_drop support that is built into JTree. To get an idea about this look at the tutorial page given by Brian Cole: How to Use Drag and Drop and Data Transfer, in the Data Transfer Support table at the top of the page. Notice that the default support for JTree is Drag_Copy, the same as for both JColorChooser and JFileChooser. In the examples given for these components, DragColorDemo in the section Importing a New Flavor: Color and DragFileDemo in the section Importing a New Flavor: Files, there was no need for a MouseMotionListener because Swing does this for us when we setDragEnabled(true) on the component. So the extra DragHandler in your JTreeTransferHandler class is fighting with Swing. The extra MouseMotionListener handler is required for components that do not have the default support as shown in the Data Transfer Support table. Here is an implementation using TransferHandlers where the FileStore class remains unchanged nad the only change in the FileStoreTransferable class is
as recommended by Brian Cole to eliminate the need for serialization.
Thanks for your precious help on this difficult DnD issue. however I'm still having problems with my applet DnD code. actually I'm trying to implement DnD on a more complex component which is TreeTable. i think this might be probably the cause of all my troubles.
I was wondering if you are willing to look at my code and help me fix this TreeTable DnD issue. If you agree, I can upload all my code to a ftp url that i can send it to you in private message.
Let me know if you kindly accept to help me on this . thanks.