Hello, Can somebody explain to me, or point me towards an example of a JTree that will dynamically change individual nodes based on criteria from a different component, such as a Resultset? For example, if you wanted each individual node icon to be set according to it's status in the database - if it's "available" it would have a green icon, if it's "not-available" it would have a red icon. I know that the TreeCellRenderer is used to do this, but as far as I can tell, it only allows one to change parts of the tree according to criteria/parameters specific to the JTree itself like, "selected", "leaf", "parent", "expanded". Is it possible to change parts of the JTree with parameters from different components? Regards, Craig Snowbarger
When you construct the nodes of your JTree you are passing a UserObject to the constructor for DefaultMutableTreeNode(). You are most likely using a String for this constructor, but that is not your only option. You can pass a custom object, too. For example, I have a recipe database. I created a recipe object which stores all of the important information of a row in the recipe table. I implemented the toString() method so the Recipe object knows how to display its self as a string. I populated my Recipe object with values from a row in the Recipe table in a database. Then I used my Recipe object as a UserObject to create a node. In this way, each node represents all of the important information found in a record in my recipe database. Is this what you meant?
Joined: Mar 07, 2001
That may be what I meant, I'm not for sure. Here is a snippet of my JTree Constructor. My JTree is created dynamically from information in a database. You'll have to forgive me for the sloppy code. I hope you can understand it. I guess my Userobject used to create my nodes are strings from my queries.
Joined: Nov 22, 2008
Yup, I feel your pain. I started out by doing mine almost exactly like that. I ended up making some changes. Here is a suggestion for a change you could make that would make your tree more OO.
Now when you get ready to do something with your node, it is actually an object of type Equipment. It knows all kinds of stuff about its self, including which icon it is supposed to use to display its self. Please excuse the sloppieness of my code.
Hi, I found this topic and I decided to continue this because I have basically same kind of problem. So how I can create JTree using Value isParent that is taken from db? I mean there is always root and after that comes those nodes who has value 0 as their isParent value. After that starts some kind of recursive loop where all the children are created. Im totally lost so please help?