gpt4 book ai didi

java - 如何防止水平滚动条在 JTree.scrollPathToVisible(TreePath) 上滚动?

转载 作者:行者123 更新时间:2023-12-02 06:16:18 32 4
gpt4 key购买 nike

虽然将树路径滚动到可见的实现起来很简单,但我想知道是否也可以滚动到路径而不干扰水平滚动条的状态或通过重置其值而不会闪烁。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class TreeScroll extends JFrame {

private JScrollPane scroll;
private JTree tree;
private DefaultTreeModel model;
private JButton button;

public TreeScroll() {
setLayout(new BorderLayout());

DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
root.add(new DefaultMutableTreeNode("filler"));
final DefaultMutableTreeNode longNode =
new DefaultMutableTreeNode("long text node is long");
root.add(longNode);
model = new DefaultTreeModel(root);

tree = new JTree(model);
scroll = new JScrollPane(tree);
add(scroll);

button = new JButton("Scroll");
add(button, BorderLayout.PAGE_END);
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
TreeNode[] pathToRoot = model.getPathToRoot(longNode);
TreePath treePath = new TreePath(pathToRoot);
tree.setSelectionPath(treePath);
tree.scrollPathToVisible(treePath);
scroll.getHorizontalScrollBar().setValue(0);
}
});

setSize(50, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
new TreeScroll().setVisible(true);
}
});
}

}

上面的例子演示了我想要做什么。单击该按钮后,程序将滚动到具有长名称的节点。由于某种原因,它尝试将尽可能多的节点标签放入其 View 中,这会导致水平滚动条滚动。我事后通过 scroll.getHorizo​​ntalScrollBar().setValue(0)“修复”了这个问题,这会引入不必要的闪烁。我想完全阻止它。有办法做到这一点吗?

这不是一个 JavaScript 问题。

最佳答案

JTree.scrollPathToVisible 旨在滚动使路径进入 View 所需的最小量,因此可能需要使用此默认行为。但是,假设您有保持水平滚动不变的特定需求,您可以实现您自己的 scrollPathToVisible 版本:

private void scrollPathToVisible(TreePath treePath) {
if (treePath != null) {
tree.makeVisible(treePath);

Rectangle bounds = tree.getPathBounds(treePath);

if (bounds != null) {
bounds.x = 0;
tree.scrollRectToVisible(bounds);
}
}
}

这只是默认实现的副本,带有将 bounds.x 设置为 0 的附加行。

关于java - 如何防止水平滚动条在 JTree.scrollPathToVisible(TreePath) 上滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432517/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com