I have nodes in JTree. I have overriden the isLeaf() in the DefaultMutableTreeNode which i have extended, For mainly creating a custom node. The requirement is on evrey expansion event of the node displayed in the tree i query the database and populate the tree. The database has fields to depict wherther the present folder has child folders. The filed is Node.childCount=0 then it has no children. I check this out and make the nodes with chilcount zero as leaf. This works fine and i get the value as false exactly. I also get that it is leaf node on calling isLeaf() But when it comes to painting the tree for the first time when i drill the query in the leaf is displayed with the + symbol to the side - i.e the Expandble icon as we see in the explorer window say + Ffolder + Folder + Leaf The plus sign by the leaf is also displayed I have a separate icon for leaf - In the TreeCellRendering - I check if it is a leaf. Then if it is a leaf i display the icon. it works fine. But I get the + sign also. Once i collapse it for the first time - then it does not reoccur. Once i have to manually do it with mouse clik. How can this Expandable icon + be not painted if leaf. I have tried firing expasion event to come over this problem - to my agony no way. it doesnt work. Can any one suggest me a way to overcome this. I dont want the + i.e the expandable icon to be displayed if it is a leaf.[because there exists no record set for a leaf] please help out!!! Thanks in advance vijay
Joined: Jul 29, 2001
Thanks pals. At lasf figured it out with help of my friend. Overriding the isLeaf() is fine and correct. I use DefaultTreeModel as the model which takes the node(the node which i have extended from DefaultMutableTreeNode) as its parameter for its constructor. The constructors are 1.DefaultTreeModel(TreeNode root) Creates a tree in which any node can have children. 2. DefaultTreeModel(TreeNode root, boolean asksAllowsChildren) Creates a tree specifying whether any node can have children, or whether only certain nodes can have children. Mistake I had commited was i used the second constructor in the wrong fashion as DefaultTreeModel dfm = DefaultTreeModel(MyNodeType,true) askAllowsChildren - a boolean, false if any node can have children, true if each node is asked to see if it can have children
DefaultTreeModel dfm = DefaultTreeModel(MyNodeType,false) This does the trick for. calling it in this fashion - forces the calling of isLeaf() function which i had overriden. Thus i get the right boolean value in my TreeCellRendering in The public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) Thus the boolean value leaf is reflected properly. I have learned a lot battling for this. Thankyou pals.
Joined: Jun 03, 2000
"vijaymagic" your name doesn't agree with the javaranch guidelines. please take a moment and re-register after reviewing the guidelines at http://www.javaranch.com/name.jsp thanks for your cooperation. - satya