Search code examples
jtree

How to select a leaf from one Jtree and display its matching results in another JTree


I have two Jtree's, In First JTree I display my directory structure which contains zip files as node child and .xlsx files as leaf. I want to add a selection event on first JTree such that when I select a leaf it should show only the matching leaf string and .xlsx files in the second JTree.


Solution

  • It's not possible to display a Node in more than one tree, since the method add(MutableTreeNode newChild) removes the given node from it's current parent node and so from its current tree, before it adds it to the new parent and so to the new tree. Create a new node on leaf selection, give it the value of the selected leaf node tree.getSelectionPath().getLastPathComponent() and add it to the other tree.