This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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.
Joined: Aug 11, 2009
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?
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.
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: