Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JTree  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each time, before calling populateTree(), we should initialize again "rootNode" and "treeModel" and set the "treeModel" to the tree:
 
Alan Brooks
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is being done already and does not help
 
Andre Uhres
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Alan Brooks wrote:This is being done already and does not help


Could you please post a short independant and compilable example?
 
Alan Brooks
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Gregg Bolinger wrote:Then changing the name of 'user' will change the name of the nodes label.


To refresh the GUI we also need to call nodeChanged:
defaultTreeModel.nodeChanged(node);
 
Alan Brooks
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I know this is still not IMO best practice but is doing this the values in the Jtree do not chnage, do you know were i am going wrong
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!