title: Filtering on a JTree
body: <h2>Problem</h2>
  
  <p>Applying filtering on a <code>JTree</code> to avoid certain nodes/leaves to show up in the rendered version of the <code>JTree</code>. Ideally I am looking for a solution which allows to have a dynamic filter, but I would already be glad if I can get a static filter to work.</p>
  
  <p>To make it a bit easier, let us suppose the <code>JTree</code> only supports rendering, and not editing. Moving, adding, removing of nodes should be possible. </p>
  
  <p>An example is a search field above a <code>JTree</code>, and on typing the <code>JTree</code> would only show the subtree with matches.</p>
  
  <p>A few restrictions: it is to be used in a project which has access to JDK and SwingX. I would like to avoid to include other third party libs.</p>
  
  <p>I already thought of a few possible solutions, but neither of those seemed ideal</p>
  
  <h2>Approaches</h2>
  
  <p><strong>Model based filtering</strong></p>
  
  <ul>
  <li>decorate the <code>TreeModel</code> to filter out some of the values. A quick-and-dirt version is easy to write. Filter out nodes, and on every change of the filter or the delegate <code>TreeModel</code> the decorator can fire an event that the whole tree has changes (<code>treeStructureChanged</code> with the root node as node). Combine this with listeners which restore the selection state and the expansion state of the <code>JTree</code> and you get a version which works more or less, but the events originating from the <code>TreeModel</code> are messed up. This is more or less the approach used in <a href="https://stackoverflow.com/questions/831813/hiding-filtering-nodes-in-a-jtree">this question</a></li>
  <li>decorate the <code>TreeModel</code> but try fire the correct events. I did not (yet) managed to come up with a working version of this. It seems to require a copy of the delegate <code>TreeModel</code> in order to be able to fire an event with the correct child indices when nodes are removed from the delegate model. I think with some more time I could get this to work, but it just feels wrong (filtering feels like something the view should do, and not the model)</li>
  <li>decorate whatever data structure was used to create the initial <code>TreeModel</code>. However, this is completely non-reusable, and probably as hard as to write a decorator for a <code>TreeModel</code></li>
  </ul>
  
  <p><strong>View based filtering</strong></p>
  
  <p>This seems like the way to go. Filtering should not affect the model but only the view. </p>
  
  <ul>
  <li><p>I took a look at <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html" rel="nofollow noreferrer"><code>RowFilter</code></a> class. Although the javadoc seems to suggest you can use it in combination with a <code>JTree</code>:</p>
  
  <blockquote>
    <p>when associated with a JTree, an entry corresponds to a node.</p>
  </blockquote>
  
  <p>I could not find any link between <code>RowFilter</code> (or <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/RowSorter.html" rel="nofollow noreferrer"><code>RowSorter</code></a>) and the <code>JTree</code> class. The standard implementations of <code>RowFilter</code> and the Swing tutorials seems to suggest that <code>RowFilter</code> can only be used directly with a <code>JTable</code> (see <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JTable.html#setRowSorter(javax.swing.RowSorter)" rel="nofollow noreferrer"><code>JTable#setRowSorter</code></a>). No similar methods are available on a <code>JTree</code></p></li>
  <li>I also looked at the <a href="http://www.jarvana.com/jarvana/view/org/swinglabs/swingx/1.6.1/swingx-1.6.1-javadoc.jar!/org/jdesktop/swingx/JXTree.html" rel="nofollow noreferrer"><code>JXTree</code></a> javadoc. It has a <a href="http://www.jarvana.com/jarvana/view/org/swinglabs/swingx/1.6.1/swingx-1.6.1-javadoc.jar!/org/jdesktop/swingx/decorator/ComponentAdapter.html" rel="nofollow noreferrer"><code>ComponentAdapter</code></a> available and the javadoc of <code>ComponentAdapter</code> indicates a <code>RowFilter</code> could interact with the target component, but I fail to see how I make the link between the <code>RowFilter</code> and the <code>JTree</code></li>
  <li>I did not yet look at how a <code>JTable</code> handles the filtering with <code>RowFilter</code>s, and perhaps the same can be done on a modified version of a <code>JTree</code>.</li>
  </ul>
  
  <p>So in short: I have no clue on what's the best approach to solve this</p>
  
  <p>Note: this question is a possible duplicate of <a href="https://stackoverflow.com/questions/831813/hiding-filtering-nodes-in-a-jtree">this question</a>, but that question is still unanswered, the question rather short and the answers seems incomplete, so I thought to post a new question. If this is not done (the FAQ did not provide a clear answer on this) I will update that 3year old question</p>