简体   繁体   中英

Hiding/filtering nodes in a JTree?

I have a data object represented in a TreeModel , and I'd like to show only part of it in my JTree --for the sake of argument, say the leaves and their parents. How can I hide/filter the unnecessary nodes?

My eventual implementation:

  • Have two TreeModel s, the underlying one and the filtered one.
  • When a change occurs on the underlying TreeModel , rebuild the filtered TreeModel from scratch. Clone each node that should be visible, and add it to its first visible ancestor in the filtered TreeModel (or the root if none are visible). See teh codez below, if you're curious.
  • This has the unfortunate side effect of collapsing every path the user had open. To get around this, I added a TreeModelListener to the filtered TreeModel . When the model changes, I save the expanded paths in the JTree (using getExpandedDescendants() ), then re-expand them later (using SwingUtilities.invokeLater() ).

    I had to override equals() in the TreeNode class I was using so that the new cloned nodes would be the same as the old cloned nodes.


  ...
  populateFilteredNode(unfilteredRoot, filteredRoot);
  ...

  void populateFilteredNode(TreeNode unfilteredNode, TreeNode filteredNode)
  {
    for (int i = 0; i < unfilteredNode.getChildCount(); i++)
    {
      TreeNode unfilteredChildNode = unfilteredNode.getChildAt(i);

      if (unfilteredChildNode.getType() == Type.INVISIBLE_FOLDER)
      {
        populateFilteredNode(unfilteredChildNode, filteredNode);
      }
      else
      {
        TreeNode filteredChildNode = unfilteredChildNode.clone();

        filteredNode.add(filteredChildNode);

        populateFilteredNode(unfilteredChildNode, filteredChildNode);
      }
    }
  }

You should be aware of GlazedLists . It's a fantastic library for doing complex table transformations with little effort. They've also expanded to trees too. It may require a little refactoring of your existing code to get it into the GlazedLists way of working. But check out the demo and the webcasts to see how powerful it is. (It's one of the essential Swing libraries in my view, and it's open source.)

Have you tried JXTree ? (unfortunately the website is down right now, but you can google for mirrors)

Take a look at this implementation: http://www.java2s.com/Code/Java/Swing-Components/InvisibleNodeTreeExample.htm

It creates subclasses of DefaultMutableNode adding a "isVisible" property rather then actually removing/adding nodes from the TreeModel.

If you're looking for a commercial solution, JideSoft has a filterable treemodel. Other than that, SwingX has a Filter API which'll work on JXTable, JXTreeTable, JXTree, and JXList.

只要它仍然是要显示一棵树,然后TreeModel是过滤器,您现有TreeModel应该足够简单。

Leverage the code you use to build your TreeNode(s) and rebuild the TreeNode(s) only including the elements you want. Set the root node on the TreeModel with the filtered root node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM