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?
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