隐藏/过滤JTree中的节点?

时间:2022-03-29 12:33:05

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?

我有一个在TreeModel中表示的数据对象,我想在我的JTree中只展示它的一部分 - 为了争论,说叶子和它们的父母。如何隐藏/过滤不必要的节点?

7 个解决方案

#1


8  

My eventual implementation:

我的最终实施:

  • Have two TreeModels, the underlying one and the filtered one.
  • 有两个TreeModel,底层的TreeModel和过滤的TreeModel。
  • 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.
  • 当底层TreeModel发生更改时,从头开始重建已过滤的TreeModel。克隆应该可见的每个节点,并将其添加到已过滤的TreeModel中的第一个可见祖先(如果没有可见的话,则将其添加到根目录)。如果你很好奇,请参阅下面的代码。
  • 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()).

    这具有折叠用户打开的每个路径的不幸副作用。为了解决这个问题,我在过滤后的TreeModel中添加了一个TreeModelListener。当模型更改时,我在JTree中保存扩展路径(使用getExpandedDescendants()),然后稍后重新展开它们(使用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.

    我必须在我正在使用的TreeNode类中重写equals(),以便新的克隆节点与旧的克隆节点相同。


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

#2


3  

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库中的一个,它是开源的。)

#3


2  

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

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

#4


1  

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有一个Filter API可以在JXTable,JXTreeTable,JXTree和JXList上运行。

#5


1  

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中删除/添加节点。

#6


0  

So long as it is still a tree you are displaying, then TreeModel that filters you existing TreeModel should be simple enough.

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

#7


0  

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上设置根节点。

#1


8  

My eventual implementation:

我的最终实施:

  • Have two TreeModels, the underlying one and the filtered one.
  • 有两个TreeModel,底层的TreeModel和过滤的TreeModel。
  • 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.
  • 当底层TreeModel发生更改时,从头开始重建已过滤的TreeModel。克隆应该可见的每个节点,并将其添加到已过滤的TreeModel中的第一个可见祖先(如果没有可见的话,则将其添加到根目录)。如果你很好奇,请参阅下面的代码。
  • 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()).

    这具有折叠用户打开的每个路径的不幸副作用。为了解决这个问题,我在过滤后的TreeModel中添加了一个TreeModelListener。当模型更改时,我在JTree中保存扩展路径(使用getExpandedDescendants()),然后稍后重新展开它们(使用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.

    我必须在我正在使用的TreeNode类中重写equals(),以便新的克隆节点与旧的克隆节点相同。


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

#2


3  

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库中的一个,它是开源的。)

#3


2  

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

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

#4


1  

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有一个Filter API可以在JXTable,JXTreeTable,JXTree和JXList上运行。

#5


1  

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中删除/添加节点。

#6


0  

So long as it is still a tree you are displaying, then TreeModel that filters you existing TreeModel should be simple enough.

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

#7


0  

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上设置根节点。