This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Java Trees and Map  RSS feed

 
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?
 
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?
 
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
Boost this thread!