Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Trees and Map

 
Pablo Mino
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pablo Mino
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 207
Eclipse IDE Firefox Browser jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it now, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic