将树节点滚动到滚动窗格的顶部

发布于 2024-10-21 08:06:17 字数 194 浏览 1 评论 0原文

I have a JTree with about more than 8 tree nodes(leafs). The requirement is if user clicks on a tree node, the selected tree node will automatically scrolls to top of the scroll pane from any position. Please help!

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

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

发布评论

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

评论(3

德意的啸 2024-10-28 08:06:17

如前所述:所有的scrollXXToVisible方法都会滚动,使得给定的XX在某处可见,它们不支持更精细的控制,因为fi“应该是可见区域中的第一个节点”。

您必须自己实现该功能,例如

TreePath path = tree.getSelectionPath();
if (path == null) return;
Rectangle bounds = tree.getPathBounds(path);
// set the height to the visible height to force the node to top 
bounds.height = tree.getVisibleRect().height;
tree.scrollRectToVisible(bounds);

“注意:这样做以响应节点上的鼠标事件可能会让用户感到烦恼,因为它会将目标从其脚下移动”。

As already noted: all scrollXXToVisible methods scroll such that the given XX is visible somewhere, they don't support finer control as f.i. "should be first node in visible area".

You have to implement that functionality yourself, something like

TreePath path = tree.getSelectionPath();
if (path == null) return;
Rectangle bounds = tree.getPathBounds(path);
// set the height to the visible height to force the node to top 
bounds.height = tree.getVisibleRect().height;
tree.scrollRectToVisible(bounds);

Beware: doing so in reaction to a mouse event on the node might be annoying to the user as it moves the target from under its feet.

皓月长歌 2024-10-28 08:06:17

在实际的JTree 上使用scrollRectToVisible 方法。

例子:

tree.scrollRectToVisible(new Rectangle(0,0));

Use the scrollRectToVisible method on the actual JTree.

Example:

tree.scrollRectToVisible(new Rectangle(0,0));
茶底世界 2024-10-28 08:06:17

这个问题很久以前就被问过,我不知道你是否还需要这个......

我理解你遇到的问题,问题是这样的:树选择侦听器不能按你的预期工作。您必须通过注册鼠标侦听器来检测单击事件。像这样:

tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    MouseListener ml = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            currentRow = selRow;
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            if (selRow != -1) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(selPath.getLastPathComponent());
                if (node != null && node.isLeaf()) {
                    String stringId = '<sting-id-of-node-you-wish-to-scroll-to>';
                    TreePath tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Forward);
                    if (tp == null) {
                        tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Backward);
                    }
                    tree.setSelectionPath(tp);
                    tree.scrollPathToVisible(tp);
                }
            }
        }
    };
    tree.addMouseListener(ml);

干杯!

This question was asked a long time ago, and I don't know if you still need this...

I understand the problem you have, the issue is this: tree selection listeners don't work as you might expect. You have to detect the click event by registering a mouse listener. Something like this:

tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    MouseListener ml = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            int selRow = tree.getRowForLocation(e.getX(), e.getY());
            currentRow = selRow;
            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
            if (selRow != -1) {
                DefaultMutableTreeNode node = new DefaultMutableTreeNode(selPath.getLastPathComponent());
                if (node != null && node.isLeaf()) {
                    String stringId = '<sting-id-of-node-you-wish-to-scroll-to>';
                    TreePath tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Forward);
                    if (tp == null) {
                        tp = tree.getNextMatch(stringId, currentRow, Position.Bias.Backward);
                    }
                    tree.setSelectionPath(tp);
                    tree.scrollPathToVisible(tp);
                }
            }
        }
    };
    tree.addMouseListener(ml);

Cheers!

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