Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTree - getting at selected node  RSS feed

 
kate damond
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of exception do you get when you try to cast it? The way that you show should work.
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!