I am trying to drag and drop tree nodes within the same JTree. I have a code which uses Java 1.2 java.awt.dnd. I would like to use TransferHandler and newer implementation. I have found a code which works when JTree is drop target but there is no code where Jtree is drag source and drop target. Can somone point good link?
JTrees are complex and there are so many choices/decisons to make and ways of doing things that it's difficult to say much about implementing drag and drop in them. Here's an example, as requested, using the j2se 1.6 improvements. Most of the code in the TransferHandler comes from various parts of the tutorial.
<blockquote>code:<pre name="code" class="core">
Wow, thanks Craig! Your code was a giant help to me to see the plentyful yapping (sorry for the sarcasm, it was of course very helpful too!) of the tutorials put together and made work by someone!
I myself was struggling to see my tree implement DnD successfully until I saw your code above.
I'd like to ask yo, if I might use part of your code in my customTreeDataHandler class? (As your code works, and mine not ... looks like my understanding of dnd still didn't reach a level where I can actually make it do what I'd like it to do )
Thanks for your answer in advance!
Joined: Jan 14, 2004
use part of your code in my customTreeDataHandler class Yes, of course.
The problem with the TreeTransferHandler is that if i drag an element of the JTree into another position in the same level, the element will be removed from the JList and will be lost.
If the element is dropped into the same level of the JTree, the element should not be removed from the JTree but leaved in the location it was without doing anything ...