Solved

Java jTree -> getting parent node

Posted on 2007-03-21
5
263 Views
Last Modified: 2012-06-27
I need to get the parent node when highlighting a child node:

node 1
---node1.1
---node1.2
---node1.3
---node1.4
node 2
---node2.1
---node2.2

So if I highlight node 2.2....
I can get that string by :

String node = jt.getSelectionPath().getLastPathComponent().toString();

But the question is... how do I get it's parent node as a String.... highlighting the child?

String parentNode = ?
0
Comment
Question by:RicardoVGomes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 18767841
String parent = jt.getSelectionPath().getPathComponent(0).toString();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18767856
What does

System.out.println(jt.getSelectionPath().getLastPathComponent());

give you?
0
 

Author Comment

by:RicardoVGomes
ID: 18767968
String parent = jt.getSelectionPath().getPathComponent(0).toString();

always gives me the same String.... parent node 1.... its suppose to give me the parent node for any child node..... so for node 2.2 ... i want node 2..... node 4.8 ... i want node 4.

------------------------------

System.out.println(jt.getSelectionPath().getLastPathComponent());

gives me the highlighted node.



0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18767991
sorry missunderstodd, then just go one level up in path

String parent = jt.getSelectionPath().getParentPath().getLastPathComponent().toString();
0
 

Author Comment

by:RicardoVGomes
ID: 18768003
Thats it.

Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
How to convert from xls to xlsx using java 7 160
passing enum to a method 4 52
Tomcat 9 + java 8 error while trying to deploy a war file 2 858
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question