简体   繁体   English

Java JTree - 如何检查节点是否显示?

[英]Java JTree - How to check if node is displayed?

Looking for how to traverse a JTree (can do that) and check to see each node to see if it's displayed (to the user) or not visible. 寻找如何遍历JTree(可以这样做)并检查每个节点是否显示(对用户)或不可见。 Can't believe JTree doesn't have this function, maybe I'm missing something? 不能相信JTree没有这个功能,也许我错过了什么?

You must consider two different things: 你必须考虑两件事:

  1. A node can become hidden by closing one of its parents. 通过关闭其父节点之一可以隐藏节点。 Even though the parent is visible on the screen, the child isn't. 即使父母在屏幕上可见,但孩子却不是。 Use JTree.isVisible() for this. 为此使用JTree.isVisible()

  2. If the node is expanded, it can become hidden because it is scrolled out of the current viewport . 如果节点已展开,则它可能会被隐藏,因为它已滚出当前视口 This isn't handled in the JTree but in the JScrollPane which wraps the tree. 这不是在JTree中处理的,而是在包装树的JScrollPane中处理的。 To find out if a node is in the visible area of the viewport. 确定节点是否位于视口的可见区域中。

To find out if #2 is true, you must get the rectangle where the node is using JTree.getPathBounds() . 要确定#2是否为true,必须获取节点使用JTree.getPathBounds()的矩形。 Then, you must intersect this rectangle with the viewport (use scrollPane.getViewport().getViewRect() . If nodeRect.intersects (viewRect) returns true , the node is visible. 然后,您必须将此矩形与视口相交(使用scrollPane.getViewport().getViewRect() 。如果nodeRect.intersects (viewRect)返回true ,则该节点可见。

Depending on your application, it may be more efficient to just look for the visible nodes, rather than iterating through all nodes in the TreeModel and determining if each is visible. 根据您的应用程序,仅查找可见节点可能更有效,而不是遍历TreeModel所有节点并确定每个TreeModel是否可见。 A sample function to perform this is shown below: 执行此操作的示例函数如下所示:

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
public class JTreeTools {
    public static List<TreeNode> getVisibleNodes(JScrollPane hostingScrollPane, JTree hostingJTree){
        //Find the first and last visible row within the scroll pane.
        final Rectangle visibleRectangle = hostingScrollPane.getViewport().getViewRect();
        final int firstRow = hostingJTree.getClosestRowForLocation(visibleRectangle.x, visibleRectangle.y);
        final int lastRow  = hostingJTree.getClosestRowForLocation(visibleRectangle.x, visibleRectangle.y + visibleRectangle.height);   
        //Iterate through each visible row, identify the object at this row, and add it to a result list.
        List<TreeNode> resultList = new ArrayList<TreeNode>();          
        for (int currentRow = firstRow; currentRow<=lastRow; currentRow++){
            TreePath currentPath = hostingJTree.getPathForRow(currentRow);
            Object lastPathObject = currentPath.getLastPathComponent();
            if (lastPathObject instanceof TreeNode){
                resultList.add((TreeNode)lastPathObject);               
            }           
        }
        return(resultList);
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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