JTree, drag n drop, easy or hard?

I have rapidly looked at some examples and some old threads about Drag磏磀rop in general and more specific for JTree磗.

A quick (maybe to quick?) conclusion is that there is a lot of code for doing this. What I want to do is to move nodes in one single JTree. Do I really need that much code or can I just implement some nice interfaces to acheive this?

[363 byte] By [sandsatera] at [2007-10-3 3:19:15]
# 1

Don't know what you've read or what code you've seen, but the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html]How to Use Drag and Drop[/url] has examples of writing simple transfer handlers for a moving rows to a different location in a JTable. So I'm sure the concept would be the same for a JTree.

camickra at 2007-7-14 21:11:09 > top of Java-index,Desktop,Core GUI APIs...
# 2

I ran over a chapter, Adding Drag-and-Drop Functionality, in a book, Pro Java Programming. I think it has been very helpful for my understanding so far. Even though I haven磘 produced any code yet.

At first, it is rather much you have to learn, but I think when all pieces come together, its quite straight forward.

A question: My intent is to create a drag-and-droppable JTree. Have I understood things right if I say that I need to create some kind of Flavor-thingie, that is a TreeNode (DefaultMutableTreeNode) flavor to make it work?

My own answer Yes if that is what I want to transfer between the drag and the drop.

Another question related to the first:

The following code is from an Image Viewer app where I think you shall be able to move images. Does every node need this or just the tree?

My own answer This is at least what I do at the tree. And since a node isnt a component, it is pretty obvious it is the tree.

protected void addNewComponent(Component comp, Point location) {

DragSource source = DragSource.getDefaultDragSource();

source.createDefaultDragGestureRecognizer(comp,

DnDConstants.ACTION_COPY_OR_MOVE,

new MyGestureListener());

comp.setLocation(location);

comp.setSize(comp.getPreferredSize());

add(comp);

repaint();

}

Message was edited by:

sandsater

sandsatera at 2007-7-14 21:11:09 > top of Java-index,Desktop,Core GUI APIs...