• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree - drag and drop inside one tree - Java 1.6

 
zdenko
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "zdenko",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use part of your code in my customTreeDataHandler class
Yes, of course.
 
Melki Joe
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Craig, your code was exactly what I was looking for.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zombie alert.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We worry a lot less about old threads than we used to. The zombie FAQ has been changed.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the heads up! I hadn't looked at that page in a long time.
 
Alain Couniot
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, actually, I have used



(Don't remember exactly why)

Alain
 
Alessio Pollero
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see DefaultMutableTreeNode#clone() returns a shallow clone. Is that what you need?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic