当JTree添加到JScrollPane时,JTree expandPath不起作用

时间:2012-09-30 20:25:12

标签: java swing jtree

我对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();

当我在没有JScrollPane的情况下将JTree直接添加到帧中时,它工作正常,这里是代码:

    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();

1 个答案:

答案 0 :(得分:4)

由于未显示捕获扩展状态的代码,因此以下可能只是一个疯狂的猜测。假设expansionStateEnumeration,可能存在一次您已经枚举其所有元素的情况,到达终点并且现在hasMoreElements()始终返回false。因此,您无法恢复状态。

考虑以下演示,该演示说明了类似的条件,但在扩展后重新获得扩展状态。或者,您可以将扩展状态缓存到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();
            }
        });
    }

}