File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Java Trees and Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Trees and Map" Watch "Java Trees and Map" New topic
Author

Java Trees and Map

Pablo Mino
Greenhorn

Joined: Aug 11, 2009
Posts: 28
Hi, I'm working with a Jtree and pulling data from a DB, essentially I was doing this



Which was good, but I got a problem since my toString() method on Object returns the PK of my entity, not the name, as names are not unique.... and I really need it to be the whole Object and not just the name attribute for later acces to its childrens

So I'm thinking of



Keep in mind those codes go inside a for loop.

Last idea doesn't seem right, as I'm creating a whole map for each registry, is there a proper way to achieve what I need?
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

If I got it right, you are creating a map only for calling its toString()?
If you want to change the node text without changing the toString() of your Folder objects then you can use convertValueToText() method in JTree.


Piyush
Pablo Mino
Greenhorn

Joined: Aug 11, 2009
Posts: 28
I kinda don't get how to use it.

What I have here is a JTree which Nodes are Folder Objects, and I need them to stay as Folder or contain a Folder

By just placing Folder inside the node it calls its toString whenever it renders a Node on JTree, so the text shows like pk=100

What I did for now is instead of using the Map, made a class called Pair which toString() returns the .getName of the Object, so I just do something like



That shows the text and lets me retrieve the Folder object later. How you do it with convertValueToText?
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Normally, we call that a Wrapper.

The default models that are found in Java are created in an generic way. The DefaultMutableTreeNode takes any kind of Object and wraps around it. If you want to use something else than the .toString, you should create yourself a FolderTreeNode or a small FolderWrapper that goes into the DefaultMutableTreeNode.
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

Pablo Mino wrote:How you do it with convertValueToText?

Whenever JTree renders a node, it gets the display text using convertValueToText method.
Default Implementation of convertValueToText() in JTree just returns value.toString(); which in turn calls userObject.toString(); which in your case is the toString() method of your Folder object.

Its pretty easy to customize the rendering. You just have to override this method in your JTree. For Example:

Pablo Mino
Greenhorn

Joined: Aug 11, 2009
Posts: 28
I get it now, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Trees and Map