wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTree - drag and drop inside one tree - Java 1.6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree - drag and drop inside one tree - Java 1.6" Watch "JTree - drag and drop inside one tree - Java 1.6" New topic
Author

JTree - drag and drop inside one tree - Java 1.6

zdenko
Greenhorn

Joined: Jul 16, 2008
Posts: 1
Hi,

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?

BR
Zdenko
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

http://www.jroller.com/santhosh/date/20050524


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Hello "zdenko",

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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">

</pre>
</blockquote>
Gregory Gawl
Greenhorn

Joined: Aug 12, 2008
Posts: 1
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!


- Gregory
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
use part of your code in my customTreeDataHandler class
Yes, of course.
Melki Joe
Greenhorn

Joined: Aug 29, 2008
Posts: 24
Hi Craig Wood,

I too have a problem in JTree that is I want to move the JTree nodes to another JTree without changing its structure. but only the selected nodes should move. Help me in this issue.

Thank you.
Evgeny Krylov
Greenhorn

Joined: Jan 25, 2009
Posts: 4
Melki Joe wrote:Hi Craig Wood,

I too have a problem in JTree that is I want to move the JTree nodes to another JTree without changing its structure. but only the selected nodes should move. Help me in this issue.

Thank you.

Hi Melki Joe .
See my solution


Evgeny Krylov
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

That's quite some kick, Evgeny, and I'm sorry to say that its intended purpose is not achieved either: Melki hasn't posted since December 16th.
Andre Uhres
Greenhorn

Joined: Nov 23, 2008
Posts: 23
Evgeny Krylov wrote:
Melki Joe wrote:Hi Craig Wood,

I too have a problem in JTree that is I want to move the JTree nodes to another JTree without changing its structure. but only the selected nodes should move. Help me in this issue.

Thank you.

Hi Melki Joe .
See my solution

Your code might be of public interest, if only it were compilable. Here ist only one example of many broken statements in your code:
Evgeny Krylov
Greenhorn

Joined: Jan 25, 2009
Posts: 4
Andre Uhres wrote:
Evgeny Krylov wrote:
Melki Joe wrote:Hi Craig Wood,

I too have a problem in JTree that is I want to move the JTree nodes to another JTree without changing its structure. but only the selected nodes should move. Help me in this issue.

Thank you.

Hi Melki Joe .
See my solution

Your code might be of public interest, if only it were compilable. Here ist only one example of many broken statements in your code:

Andre, thanks for comment.
It was my bug of my web-publishing tool.
Now working
my solution
Mike Dee
Greenhorn

Joined: Dec 26, 2002
Posts: 4
Thanks Craig, your code was exactly what I was looking for.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Zombie alert.


luck, db
There are no new questions, but there may be new answers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
We worry a lot less about old threads than we used to. The zombie FAQ has been changed.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Thanks for the heads up! I hadn't looked at that page in a long time.
Alain Couniot
Greenhorn

Joined: Jan 31, 2011
Posts: 2
Very nice illustration and almost perfect! Thanks, Craig! It works but there is nevertheless a subtle bug (in the method below)



As it is, as you multiply the moves, it repeatedly wraps nodes into a new node (instead of only its userObject).

A correct implementation would be, e.g.,

Based on this working example, I have cleaned up a messy class of mine (with more generic purposes) that had become unmaintainable.

A big thank again !
Alain
Alain Couniot
Greenhorn

Joined: Jan 31, 2011
Posts: 2
Well, actually, I have used



(Don't remember exactly why)

Alain
Alessio Pollero
Greenhorn

Joined: Jan 17, 2012
Posts: 1
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 ...
Adolfo Gomez Cala
Greenhorn

Joined: May 31, 2014
Posts: 2
Alain Couniot wrote:Well, actually, I have used



(Don't remember exactly why)

Alain


In good faith the actual code should be



or your code would not compile...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Welcome to the Ranch

I see DefaultMutableTreeNode#clone() returns a shallow clone. Is that what you need?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree - drag and drop inside one tree - Java 1.6
 
Similar Threads
JTree
DND from JTree to JTable
JTree DropTargetListener not working
Strange problem -- Drag and Drop with JTree
Editable JTree????