java 在 JTree 中隐藏/过滤节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/831813/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:57:35  来源:igfitidea点击:

Hiding/filtering nodes in a JTree?

javaswingjtree

提问by Amanda S

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?

我有一个用 a 表示的数据对象TreeModelJTree为了便于论证,我只想在我的中显示它的一部分,比如叶子和它们的父母。如何隐藏/过滤不必要的节点?

回答by Amanda S

My eventual implementation:

我的最终实现:

  • Have two TreeModels, the underlying one and the filtered one.
  • When a change occurs on the underlying TreeModel, rebuild the filtered TreeModelfrom 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 TreeModelListenerto 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 TreeNodeclass I was using so that the new cloned nodes would be the same as the old cloned nodes.

  • 有两个TreeModels,底层的一个和过滤的一个。
  • 当底层发生变化时TreeModelTreeModel从头开始重建过滤。克隆每个应该可见的节点,并将其添加到过滤器中的第一个可见祖先TreeModel(如果不可见,则添加到根)。如果您好奇,请参阅下面的代码。
  • 这具有折叠用户打开的每条路径的不幸副作用。为了解决这个问题,我TreeModelListener在过滤后的TreeModel. 当模型更改时,我将展开的路径保存在JTree(使用getExpandedDescendants())中,然后稍后重新展开它们(使用SwingUtilities.invokeLater())。

    我必须equals()TreeNode我使用的类中进行覆盖,以便新的克隆节点与旧的克隆节点相同。



  ...
  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);
      }
    }
  }

回答by arooaroo

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.)

你应该知道GlazedLists。这是一个很棒的库,可以轻松完成复杂的表转换。他们也扩展到树木。可能需要对现有代码进行一些重构才能使其进入 GlazedLists 的工作方式。但请查看演示和网络广播,了解它的强大功能。(在我看来,它是必不可少的 Swing 库之一,并且是开源的。)

回答by Davide

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

你试过JXTree吗?(不幸的是,该网站现在已关闭,但您可以谷歌搜索镜像)

回答by Aakash

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.

如果您正在寻找商业解决方案,JideSoft 有一个可过滤的树模型。除此之外,SwingX 有一个过滤器 API,它可以在 JXTable、JXTreeTable、JXTree 和 JXList 上工作。

回答by Martin Wickman

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

看看这个实现: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.

它创建 DefaultMutableNode 的子类,添加“isVisible”属性,而不是实际从 TreeModel 中删除/添加节点。

回答by Tom Hawtin - tackline

So long as it is still a tree you are displaying, then TreeModelthat filters you existing TreeModelshould be simple enough.

只要它仍然是您正在显示的树,那么TreeModel您现有的过滤器TreeModel应该足够简单。

回答by user101884

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.

利用您用于构建 TreeNode 的代码并仅包含您想要的元素重建 TreeNode。使用过滤后的根节点在 TreeModel 上设置根节点。