Solved

Java jTree -> getting parent node

Posted on 2007-03-21
5
258 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
  • 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 84
replace a word with other 1 44
map related example 6 49
login form jsp example 2 26
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

832 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