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
Solved

Java jTree -> getting parent node

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 35
Chrome and Firefox Java 5 51
java imports not found 4 26
Java 8 to Java 6 8 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

861 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