posted 9 years ago
Hello,
I have been making a dynamic JTree but when i delet a node it disapears from the tree but when i click to expand the new tree the node that has been removed reapers. Can anyone help me so that when i remove the node it does not come back when i try expand the tree.
I am using
I have been making a dynamic JTree but when i delet a node it disapears from the tree but when i click to expand the new tree the node that has been removed reapers. Can anyone help me so that when i remove the node it does not come back when i try expand the tree.
I am using
Andre Uhres
Greenhorn
Posts: 23
Alan Brooks
Greenhorn
Posts: 14
posted 9 years ago
I noticed you are passing the name of the User object to the constructor of DefaultMutableTreeNode. This works, however, for renaming and IMO best practice dictates that you specify a custom User Object in the constructor of your node. The node will then use the toString method of your object for the node label.
subFolder = new DefaultMutableTreeNode(getUser(a));
And then assuming User's toString returns getName(), you're good. Anytime you want access to this object you can simple do:
User user = (User)node.getUserObject();
Then changing the name of 'user' will change the name of the nodes label.
Alan Brooks wrote:Hey managed to get the JTree to update I was not redrawing it the correct way. I have one last question. If you use a treeModel is there anyway to rename the node after it has been inserted.
I noticed you are passing the name of the User object to the constructor of DefaultMutableTreeNode. This works, however, for renaming and IMO best practice dictates that you specify a custom User Object in the constructor of your node. The node will then use the toString method of your object for the node label.
subFolder = new DefaultMutableTreeNode(getUser(a));
And then assuming User's toString returns getName(), you're good. Anytime you want access to this object you can simple do:
User user = (User)node.getUserObject();
Then changing the name of 'user' will change the name of the nodes label.
Andre Uhres
Greenhorn
Posts: 23
