I have a JTree with custom objects and custom model.
At some point, I select a node and when that happens, I update the tree with new retrieved data.
When that happens I go trough the tree to find selected node and substitute it by the new one (up to date).
When I find it, I remove the old node from its parent, add the new one in its positions and call nodeChanged(newNode). The tree updates ok, the new node appears there with updated content.
The problem is when coming back from this tree update, the selection paths have not been updated, so when I use method getSelectionPaths(), the returning path (if only one node is selected) corresponds to the old node that I removed from the tree.
How can I update the selection paths to the new updated model?
You could create a new TreePath and call setSelectedPath with the new path. However, what would be better is to, instead of removing the node, make it mutable and update the node. This way the tree model isn’t changing and the selection path wouldn’t be changed.
You’d also need to fire the appropriate event (node changed, instead of node removed/added etc).