Home arrow static arrow Java Programming [Archive] - drag and drop with JTree
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - drag and drop with JTree
This topic has 4 replies on 1 page.

Posts:208
Registered: 4/23/03
drag and drop with JTree  
Jun 16, 2004 3:38 AM



 
im having a problem with drag and drop

my drag is perfect, and a string is passed to a transferable, which is to be dropped into a jtree.
what is supposed to happen is that when the drop happens, the jtree should create a new leaf with the string name which is dropped, and put the leaf under the node where the drop occured.
however this is not happening. the drop happens, but it falls under whichever node it feels like!

there are duke dollars for the right solution(s)...

here is the code:

this is my class beginning, which shows the interfaces and the superclass

public class TreePanel extends JTree implements DropTargetListener, TreeSelectionListener{


this is the drop method which handles the drop.

public void drop(DropTargetDropEvent dtde) {		try {			selectedNode = tML.dmtn;		} catch(Exception e) {			System.out.println("No available node");		}		try {			DataFlavor stringFlavor = DataFlavor.stringFlavor;			Transferable tr = dtde.getTransferable();			if(dtde.isDataFlavorSupported(stringFlavor)) {				String filename = (String) tr.getTransferData(stringFlavor);				try {					URL picture = new URL(filename);					addImageToNode(selectedNode, picture);									}catch(Exception e){					e.printStackTrace();				}								dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);				dtde.dropComplete(true);			}			else {				dtde.rejectDrop();				System.out.println("Drop rejected");			}		}		catch(UnsupportedFlavorException ufe) {			ufe.printStackTrace();		}		catch(IOException ioe) {			ioe.printStackTrace();		}				}


this is another class which is used as a mousemotionlistener, and which detects the location of where exactly in the tree the drop is occuring.
class TreeMouseListener extends MouseMotionAdapter {	DefaultMutableTreeNode dmtn;		public void mouseMoved(MouseEvent me) {		try{				JTree tree = (JTree) me.getSource();			TreePath path = tree.getPathForLocation(me.getX(), me.getY());			dmtn = (DefaultMutableTreeNode)path.getLastPathComponent();		} catch(Exception e) {		}	}}


this bit is in the constructor to make an instance of TreeMouseListener, so that the drop method can use it.
tML = new TreeMouseListener();this.addMouseMotionListener(tML);

this is the method which is supposed to add the given string to the node where the drop occurs:

public void addImageToNode(DefaultMutableTreeNode givenNode, URL pictureLocation) {		if(givenNode.getAllowsChildren()) {			DefaultMutableTreeNode image = new DefaultMutableTreeNode(pictureLocation, false);			givenNode.add(image);			show();		}		else {			System.out.println("This node does not allow children");		}		System.out.println(selectedNode + " at " + pictureLocation);			}

can you give me any reason why the drop is not occuring where it should occur, (the DefaultMutableTreeNode where it is dropped) and why it is coming up in some other DefaultMutableTreeNode?
its kind of supposed to work like in Windows Explorer, where you can drag and drop files onto the folder list on the left, except when you drop a file onto My Documents, its turning up instead in My Music!!!

 

Posts:31,095
Registered: 4/30/99
Re: drag and drop with JTree  
Jun 16, 2004 8:32 AM (reply 1 of 4)



 
selectedNode = tML.dmtn;
That's the line of code where you decide what node to attach the dropped content. And you're saying that isn't the right node. Then you'll have to change that line of code so it does select the right node. My approach would be to start with your DropTargetDropEvent, which contains the information about where the drop is to take place. It has a getLocation() method which returns a Point object. I would break that up into its x and y components and feed them to the JTree's getClosestPathForLocation() method, which gives you a TreePath, and so on. That looks to me like it should work properly.

PC²
 

Posts:208
Registered: 4/23/03
Re: drag and drop with JTree  
Jun 16, 2004 10:00 PM (reply 2 of 4)



 
THANKS MAN

What a lifesaver! that completely solved all the hitches i had!
If i could kiss you i would!!!

take some dukes! how many ever you want!
 

Posts:208
Registered: 4/23/03
Re: drag and drop with JTree  
Jun 18, 2004 1:54 AM (reply 3 of 4)



 
well i now have another problem
[code]
ancestorTree.removeSelectionPath(path);
parentNode.removeAllChildren();
parentNode.removeFromParent();
[/code[
ancestorTree is the JTree, and parentNode is a DefaultMutableTreeNode which has to be removed from the tree. however the code that is presented here does not cause the node to be removed from the tree. however it is removed from being a child of its parent, but it is not removed from the screen. it still responds to mouse clicks, selections etc. so it is not a painting problem.

how does one remove a node from the jtree?
 

Posts:208
Registered: 4/23/03
Re: drag and drop with JTree  
Jun 18, 2004 1:55 AM (reply 4 of 4)



 
sorry about that.

let me repeat my end statement:

ancestorTree is the JTree, and parentNode is a DefaultMutableTreeNode which has to be removed from the tree. however the code that is presented here does not cause the node to be removed from the tree. however it is removed from being a child of its parent, but it is not removed from the screen. it still responds to mouse clicks, selections etc. so it is not a painting problem.how does one remove a node from the jtree?
 
This topic has 4 replies on 1 page.