This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes how do I get the current node selection in a Jtree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do I get the current node selection in a Jtree?" Watch "how do I get the current node selection in a Jtree?" New topic
Author

how do I get the current node selection in a Jtree?

Juliana Steven
Greenhorn

Joined: Nov 08, 2009
Posts: 6
Hi,

I have been trying to figure out how to access the current node in a JTree. I have a main class where the tree is defined. When the add button is pressed, the add method from another class is to be invoked. But the listener is defined in the main class, how do I pass the variable to this other class?




Juliana Steven
Greenhorn

Joined: Nov 08, 2009
Posts: 6
At this point, I am just trying to see the node content. This piece of code, gives null all the time. I cannot proceed to add till I can identify the node where I have to insert.

Please help.
Thanks,
Juliana
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
Does this:

#
# DefaultMutableTreeNode node = null;
# DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();

compile?
Juliana Steven
Greenhorn

Joined: Nov 08, 2009
Posts: 6
Hello Albareto,
Well actually, I have been modifying and test the code so much, I think I put an older version here. The 2nd statement should be just
Regards,
Juliana
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
I don't really understand the flow of your application, who calls to actionPerformed? What's the value of the tree at that point? What's doing tree.getLastSelectedPathComponent and why is it returning null?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1752
    
    7

There's a good little tutorial about how to use the JTree component over at Sun.
It covers node selection and retrieval of data from tree nodes. Have a look.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
 
 
subject: how do I get the current node selection in a Jtree?
 
Similar Threads
Node labelling within a tree using depth-first traversal
render jtree in jtable
Jtree adding nodes
JTree
Help JTree is not displaying inside of JFrame