获取不在视口中的 JTree 节点的行或 TreePath

发布于 2024-10-08 11:22:03 字数 499 浏览 4 评论 0原文

我有一个 JTree 和一个自定义渲染器。

JTree 是按交换机和端口号组成的 IP 地址树。

我有一张 IP 地址图。

当用户单击图形线时,我尝试更新 JTree 以显示用户选择的线。

如果用户在图表中单击鼠标选择了 IP,则渲染器将包含该节点,并且其他操作很容易发生,因为行和 TreePath 可用。

如果 ip 列表很大并且有些已滚动出视口,则我在查找我想要选择并在视口中显示的对象/ip 的行或树节点时遇到问题。

我在 JTree 中有一个 ActionEvent,它获取从 getUserObject() 返回的一些组成节点文本的文本。

我不知道如何找到行或 TreeNode。如果我能找到它们,我可以检查该行是否可见,如果不可见,则调用 makeVisible(treePath) - 或使用scrollPane.getViewport().getViewRect() 并将其与树节点的矩形进行比较。

感谢您的任何提示或方向... 吉姆

I have a JTree and a custom renderer.

The JTree is a tree of ip addresses by switch and port number.

I have a graph of the ip addresses.

When the user clicks on a graph line, I try to update the JTree to show the line selected by the user.

If the ip selected by the user's mouse click in the graph, the renderer contains that node and other actions happen easily because the row and TreePath are available.

If the list of ip's is large and some are scrolled out of the viewport, I am having a problem finding out what the row or treenode is for the object/ip that I want to select and show in the viewport.

I have an ActionEvent in the JTree that gets some of the text that makes up the nodes' text, returned from getUserObject().

I don't know how to find the row or TreeNode. If I could find them I could check if the row was visible and if not call makeVisible(treePath) - or use scrollPane.getViewport().getViewRect() and compare it to the rect of the treeNode.

Thanks for any tips or direction ...
Jim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送你一个梦 2024-10-15 11:22:03

我通过以下方式解决了这个问题。

展开所有节点后,我填充一个以 ip 作为键、以 row 作为值的哈希图。

当我收到包含 ip 的事件时,我会在 hasmap 中查找该行。

无论节点/行是否在视口中,这都有效。

如下:

        if ( ipRowMap.containsKey( ip )) {
            row = ipRowMap.get( ip ); 
            TreePath tp = getPathForRow( row );
            Rectangle rtp = this.getPathBounds( tp );
            Rectangle rsp = scrollPane.getViewport().getViewRect();
            if ( !rtp.intersects( rsp)) {
                scrollPane.getViewport().scrollRectToVisible( rtp );
            }
        }

第一次有效,但不是每次都有效。

但如果我不能解决这个问题,那将是一个新帖子。我认为滚动窗格或视口已经以我无法处理的方式发生了变化。

I solved the problem in the following manner.

After expanding all the nodes I fill a hashmap that has ip as the key and row as the value.

When I get me event containing the ip, I look up the row in the hasmap.

This works whether the node/row is in the viewport or not.

As follows:

        if ( ipRowMap.containsKey( ip )) {
            row = ipRowMap.get( ip ); 
            TreePath tp = getPathForRow( row );
            Rectangle rtp = this.getPathBounds( tp );
            Rectangle rsp = scrollPane.getViewport().getViewRect();
            if ( !rtp.intersects( rsp)) {
                scrollPane.getViewport().scrollRectToVisible( rtp );
            }
        }

This works the first time, but not every time.

But that will be a new post if I can't solve that problem. I think the scrollpane or viewport has changed in a way I am not handling.

流云如水 2024-10-15 11:22:03

我找到了一种更好的使用 JTree 的方法。

上面的方法适用于 JList。

由于我有一个行映射,给定行号,有一个 JTree 特定的 Component 调用。

            scrollRowToVisible( row );

代码变成

    if ( ipRowMap.containsKey( ip )) {
        row = ipRowMap.get( ip ); 
        scrollRowToVisible( row );
    }

I found a better method for working with a JTree.

The method above works ok for JLists.

Since I have a map of rows, given the row number there is a JTree specific call of Component.

            scrollRowToVisible( row );

and the code becomes

    if ( ipRowMap.containsKey( ip )) {
        row = ipRowMap.get( ip ); 
        scrollRowToVisible( row );
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文