• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this:

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

compile?
 
Juliana Steven
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic