File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Displaying Data in a JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying Data in a JTree" Watch "Displaying Data in a JTree" New topic

Displaying Data in a JTree

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
I have a JTree that is displaying part numbers. I have had a request from users that I also display the description of the part number. Is there a way that I can display 2 columns of data on one tree node? Otherwise, the only other way I know how to do this is to get the description and concatenate it with the part number. Which would cause me problems as I have a popup menu attached to the tree nodes that takes the value of the node (part number) and passes it to another application. How would I be able to tell it that I only want the part number to be passed and not the description?

Thanks for any help!
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I suppose the objects in your tree nodes are just Strings containing the part numbers? I would do this:

1. Create a Part object containing the part number and part name. Make the toString() method of Part return the part number and name concatenated, so that's what will appear in the JTree.

2. Change the popup menu to get the part number from the Part object that's in the node.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the response! That makes complete sense to me now. You're awesome!!!
I agree. Here's the link:
subject: Displaying Data in a JTree
It's not a secret anymore!