This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JTree - drag and drop inside one tree - Java 1.6

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jroller.com/santhosh/date/20050524
 
Sheriff
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "zdenko",

Please read your private messages regarding an important announcement.

Thank you,

Rob
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use part of your code in my customTreeDataHandler class
Yes, of course.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22817
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Craig, your code was exactly what I was looking for.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Zombie alert.
 
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the heads up! I hadn't looked at that page in a long time.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, actually, I have used



(Don't remember exactly why)

Alain
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80085
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I see DefaultMutableTreeNode#clone() returns a shallow clone. Is that what you need?
 
Destroy anything that stands in your way. Except this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic