Solved

Java jTree -> getting parent node

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now