I've implemented a custom TreeModel that doesn't extend DefaultTreeModel. How do I get a JTree to update when the model changes?
Created May 7, 2012
Scott Stanchfield
Take a look at javax.swing.DefaultTreeModel. In particular, methods:
- addTreeModelListener()
- removeTreeModelListener()
- fireTreeNodesChanged()
- fireTreeNodesInserted()
- fireTreeNodesRemoved()
- fireTreeStructureChanged()
- nodesChanged()
- nodesInserted()
- nodesRemoved()
- nodeStructureChanged()
and the use of the listenerList declared in it.
You need to add similar event firing mechanisms in your model.
When you set your model to be the model that a JTree uses, the JTree adds itself as a TreeModelListener. Anytime you make a change in your model, fire an event (using one of the above methods that you'll create in your model) and the JTree will receive notification, and update itself.