tags:

views:

76

answers:

2

I've got a JTree which I'm using to display some (unsurprisingly) hierarchical data. Part of the spec is that the user can change the data source (atm it's just between files). Now, when this happens, I can rebuild the data and the tree nodes with no problem. But, I'm having substantial difficulties getting the tree to update the changes. I tried removing it from it's scrollpane and replacing with a new JTree, but I didn't see any such. I've tried removing all from the JTree and didn't see any effect.

How can I make the JTree display changes after it's been constructed?
Ninjedit: Yes, I did call updateUI().

Another edit: I also wanted to replace the tree's current data with my new data. However, I don't see any methods that will take the DefaultMutableTreeNode that I constructed with. Even if I just remove the JTree and call updateUI on it's containing ScrollPane, nothing happens. Or if I use repaint instead.

A: 

try calling the repaint method on the JComponent (JTree in your case) after you manipulated the TreeModel:

http://download.oracle.com/javase/1.5.0/docs/api/java/awt/Component.html#repaint%28%29

smeg4brains
No change. Thanks for the suggestion.
DeadMG
+1  A: 

It could be that the proper events (the JTree internal events) are not being fired. For example, you can add nodes either by using node.add(...) or even better, model.insertNodeInto(...) (assuming you're using the DefaultTreeModel). In this case, the latter method is preferred as it will fire appropriate events that will cause the view (the JTree) to update correctly. It's possible that your problem isn't with redrawing the UI, but in fact notifying the view that the model has changed.
So, I would suggest looking in to how your dynamically modifying your JTree, and if possible I'd suggest using the DefaultTreeModel as your model to drive the view.

And just to make sure, you've read through the Sun JTree tutorials, right?

Shakedown
Turns out that the defaultTreeModel contained all the functions I needed. The JTree just didn't provide any useful methods to get at them. Once I made the JTree from a model and grabbed the model, all was easy.
DeadMG
Excellent! It pays to be model-driven.
Shakedown