This week's book giveaways are in the C/C++ and Spring forums.
We're giving away four copies each of C++ Concurrency and Spring Integration in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT / JFace and the fly likes JTree - drag and drop inside one tree - Java 1.6 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of C++ Concurrency this week in the C/C++ forum
or Spring Integration in Action in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply 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: 7103

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


[Donate a pint, save a life!] [How to ask questions]
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 17350

Hello "zdenko",

Please read your private messages regarding an important announcement.

Thank you,

Rob


SCJP 1.4 - SCJP 6 - SCWCD 5
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>

This message was edited 1 time. Last update was at by Campbell Ritchie

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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 17350

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: 2600

Zombie alert.



luck, db
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 25245
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: 2600

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 ...
 
 
subject: JTree - drag and drop inside one tree - Java 1.6
 
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com