File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT / JFace and the fly likes JTree - getting at selected node Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT / JFace
Reply Bookmark "JTree - getting at selected node" Watch "JTree - getting at selected node" New topic
Author

JTree - getting at selected node

kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
This should be really easy. Each node in my JTree has several attributes which I now want to get at. I can get the selected path for my tree using getSelectionPath(), but I don't seem to be able to get at the node itself !!
Thanks,
Kate
kate damond
Ranch Hand

Joined: Apr 17, 2001
Posts: 77
Ok, I figured that out now.

Next question is that I don't seem to be able to get at my nodes correctly. I get a DefaultMutableTreeNode, but I know that my nodes were MyTreeNodeObject when I put them there.
So, how comes I can't cast back to MyTreeNodeObject ???


Cheers
Kate
John Gallagher
Greenhorn

Joined: Aug 21, 2001
Posts: 6
What kind of exception do you get when you try to cast it? The way that you show should work.
Vinod Venugopal
Ranch Hand

Joined: Dec 06, 2000
Posts: 148
Why dont u put all ur nodes ( custom ) in a Vector, then run a search & when u get what u want pick it from the collection.
This is how u would cast it to ur custom node type...
DefaultMutableTreeNode currentNode =(DefaultMutableTreeNode)Tree.getLastSelectedPathComponent();
....
CustomNodeType currentItem = (CustomNodeType)currentNode.getUserObject();
this shud work


- Vinod<br />-------<br />SCJP2
 
 
subject: JTree - getting at selected node
 
Threads others viewed
JTree class
JTree
JTree
JTree and JEditorPane
swing editing JTree nodes
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com