I'd like to have several JTree
s (all with very similar content) have their selected nodes in sync (when the same node exists in each tree).
At the moment I'm doing this by adding a TreeSelectionListener
to each tree and calling setSelectedPath
on the other trees (if the getSelectedPaths
doesn't contain the path).
However, I've seen this lead to infinite cycles, where each tree is repeatedly selecting different nodes (I'm not sure how to reproduce this, but I've seen it when dragging nodes).
There doesn't appear to be a way to set the selected path without triggering the tree selection listener, and I'd rather not go down the path of trying to remove listeners.
I'm considering the possibility of sharing a TreeSelectionModel
between the trees, but this looks like a lot of work given the size of DefaultTreeSelectionModel
.
Are there any other ways to achieve the same result?
Add a flag isAPI. Set the flag when you programmatically select paths in the trees and reset it after path is set.
In the listeners just check whether the isAPI==true and return from the listener.