How can I make a JTree see the same data as another JTree?

Scott Stanchfield

The answer is simple: Share their models!

You can do this by setting the model of the second tree to the model of the first tree. For example:

JTree tree1 = new JTree();
// load data into tree1 however you currently do

JTree tree2 = new JTree(tree1.getModel());

If you set up the model separately, use code like the following:

TreeModel model = ...;

JTree tree1 = new JTree(model);
JTree tree2 = new JTree(model);

This will cause the two trees to share the same data, but allow separate selections. If you want to use the same selections in both trees, you can also share the selection model:
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact