gpt4 book ai didi

java - 将 JTree 添加到 JScrollPane 时,JTree ExpandPath 不起作用

转载 作者:行者123 更新时间:2023-12-01 18:59:42 26 4
gpt4 key购买 nike

我在扩展 JTree 时遇到了奇怪的问题。我尝试将 JTree 添加到 JScrollPane。
树应该恢复到展开状态,但它不起作用 - 树已折叠。
代码如下:

   if (expansionState != null) {
while (expansionState.hasMoreElements()) {
TreePath treePath = (TreePath) expansionState.nextElement();
tree.expandPath(treePath);
}
}

m_Scroll = new JScrollPane(tree);
m_Scroll.setBounds(100, 100, 800, 200);
mainFrame.getContentPane().add(m_Scroll);
mainFrame.revalidate();

当我将 JTree 直接添加到框架而不使用 JScrollPane 时,它​​工作正常,代码如下:

    if (expansionState != null) {
while (expansionState.hasMoreElements()) {
TreePath treePath = (TreePath) expansionState.nextElement();
tree.expandPath(treePath);
}
}

//m_Scroll = new JScrollPane(tree);
tree.setBounds(100, 100, 800, 200);
mainFrame.getContentPane().add(tree);
mainFrame.revalidate();

最佳答案

由于未显示捕获扩展状态的代码,因此以下内容可能只是一个疯狂的猜测。假设 expansionStateEnumeration 是否存在这样的情况:您已经枚举了一次其所有元素,到达了结尾,现在 hasMoreElements() 总是返回错误的。因此,您无法恢复状态。

考虑以下演示,它说明了类似的条件,但在扩展后重新捕获扩展状态。或者,您可以将扩展状态缓存到 List 或类似的集合中。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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.TreePath;

public class TestTree {
private Enumeration<TreePath> expansionState;

public TestTree() {
JFrame frame = new JFrame("TestTree");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);

final JTree tree = new JTree(buildDemoModel());

JPanel buttonsPanel = new JPanel();

JButton saveButton = new JButton("Capture state");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
expansionState = saveExpansionState(tree);
}
});

JButton loadButton = new JButton("Load state");
loadButton.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
loadExpansionState(tree, expansionState);
//recapture the expansion state
expansionState = saveExpansionState(tree);
}

});

buttonsPanel.add(saveButton);
buttonsPanel.add(loadButton);

JPanel content = new JPanel(new BorderLayout());
content.add(buttonsPanel, BorderLayout.SOUTH);
content.add(new JScrollPane(tree), BorderLayout.CENTER);

frame.add(content);
frame.pack();
frame.setVisible(true);
}

public static Enumeration<TreePath> saveExpansionState(JTree tree) {
return tree.getExpandedDescendants(new TreePath(tree.getModel().getRoot()));
}

public static void loadExpansionState(JTree tree, Enumeration<TreePath> enumeration) {
if (enumeration != null) {
while (enumeration.hasMoreElements()) {
TreePath treePath = enumeration.nextElement();
tree.expandPath(treePath);
}
}
}

public static DefaultTreeModel buildDemoModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode("A");
childNode.add(new DefaultMutableTreeNode("A1"));
childNode.add(new DefaultMutableTreeNode("A2"));
root.add(childNode);

childNode = new DefaultMutableTreeNode("B");
childNode.add(new DefaultMutableTreeNode("B1"));
childNode.add(new DefaultMutableTreeNode("B2"));
root.add(childNode);

return new DefaultTreeModel(root);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestTree();
}
});
}

}

关于java - 将 JTree 添加到 JScrollPane 时,JTree ExpandPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12664829/

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