在设置新的TreeModel时如何自动扩展JTree?

时间:2013-03-04 20:57:28

标签: java swing tree jtree

我有自定义JTree和自定义JModel;当我给它一个新的模型时,我会让JTree“自动扩展”。目前,它只是将所有节点折叠到根目录。

以下是一个例子:

private class CustomTree extends JTree {

    @Override
    public boolean isExpanded(TreePath path) {
        return ((Person) path.getLastPathComponent).hasChildren();

}

private class CustomTreeModel extends TreeModel {

    // ... omitting various implementation details

    @Override
    public boolean isLeaf(Object object) {
        return !((Person) object).hasChildren();
    }

}

Model model = new Model();
Person bob = new Person();
Person alice = new Person();
bob.addChild(alice);
model.setRoot(bob);
JTree tree = new CustomTree(new CustomTreeModel(model));

此时,树正确显示:

- BOB
  - ALICE

其中Alice是Bob的孩子(在数据和可视化树中)

但是,如果我打电话:

tree.setModel(new CustomTreeModel(model));

一切都崩溃了:

+ BOB

在设置新模型时,有没有办法“自动展开”树中的所有内容?

7 个答案:

答案 0 :(得分:34)

以下对我有用(在设置新模型后调用):

for (int i = 0; i < tree.getRowCount(); i++) {
    tree.expandRow(i);
}

答案 1 :(得分:19)

我遇到了类似的问题。

您的解决方案(已发布https://stackoverflow.com/a/15211697/837530)似乎仅适用于顶级树节点。

但我需要扩展所有后代节点。所以我用下面的递归方法解决了它:

private void expandAllNodes(JTree tree, int startingIndex, int rowCount){
    for(int i=startingIndex;i<rowCount;++i){
        tree.expandRow(i);
    }

    if(tree.getRowCount()!=rowCount){
        expandAllNodes(tree, rowCount, tree.getRowCount());
    }
}

调用
expandAllNodes(tree, 0, tree.getRowCount());

其中,treeJTree

除非有人有更好的解决方案。

答案 2 :(得分:8)

还有这种非递归版本。

private void expandAllNodes(JTree tree) {
    int j = tree.getRowCount();
    int i = 0;
    while(i < j) {
        tree.expandRow(i);
        i += 1;
        j = tree.getRowCount();
    }
}

答案 3 :(得分:1)

这对我有用..

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeNode;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Enumeration;

public class JTreeNodeAutoExpandCollapse extends JFrame {
    public JTreeNodeAutoExpandCollapse() throws HeadlessException {
        initializeUI();
    }

    private void initializeUI() {
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode chapter1 = new DefaultMutableTreeNode("Chapter 1");
        DefaultMutableTreeNode sub1 = new DefaultMutableTreeNode("1.1");
        DefaultMutableTreeNode sub2 = new DefaultMutableTreeNode("1.2");
        DefaultMutableTreeNode sub3 = new DefaultMutableTreeNode("1.3");
        DefaultMutableTreeNode sub31 = new DefaultMutableTreeNode("1.3.1");
        DefaultMutableTreeNode sub32 = new DefaultMutableTreeNode("1.3.2");

        root.add(chapter1);
        chapter1.add(sub1);
        chapter1.add(sub2);
        chapter1.add(sub3);
        sub3.add(sub31);
        sub3.add(sub32);

        final JTree tree = new JTree(root);
        expandTree(tree, false);

        JScrollPane pane = new JScrollPane(tree);
        pane.setPreferredSize(new Dimension(200, 200));

        JPanel buttonPanel = new JPanel(new BorderLayout());
        JButton expandAll = new JButton("Expand All");
        expandAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                expandTree(tree, true);
            }
        });

        JButton collapseAll = new JButton("Collapse All");
        collapseAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                expandTree(tree, false);
            }
        });

        buttonPanel.add(expandAll, BorderLayout.WEST);
        buttonPanel.add(collapseAll, BorderLayout.EAST);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(pane, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    private void expandTree(JTree tree, boolean expand) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }

    private void expandAll(JTree tree, TreePath path, boolean expand) {
        TreeNode node = (TreeNode) path.getLastPathComponent();

        if (node.getChildCount() >= 0) {
            Enumeration enumeration = node.children();
            while (enumeration.hasMoreElements()) {
                TreeNode n = (TreeNode) enumeration.nextElement();
                TreePath p = path.pathByAddingChild(n);

                expandAll(tree, p, expand);
            }
        }

        if (expand) {
            tree.expandPath(path);
        } else {
            tree.collapsePath(path);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JTreeNodeAutoExpandCollapse().setVisible(true);
            }
        });
    }
}

答案 4 :(得分:0)

由于我无法编辑或评论接受OP的最后一个问题的答案:

  

有没有办法自动扩展&#34;设置新模型时树中的所有内容?

要在分配新模型后(根据OP的请求)展开树,请覆盖自定义树中的treeDidChange()

@Override 
public void treeDidChange() { 
    super.treeDidChange(); 
    expandAllNodes(this, 0, getRowCount());
} 

答案 5 :(得分:0)

public void expandTree(){
    int row = 1;
    while (row++ < tree.getRowCount()){
        tree.expandRow(row);
    }

public void collapseTree(){
    int row = tree.getRowCount() - 1;
    while (row-- > 0){
        tree.collapseRow(row);
    }
}

答案 6 :(得分:-3)

TreeUtils.expandAll(tree, true);

TreeUtils你会从jidesoft获得。 这不是开源库,但是如果你有它,你可以简单地扩展所有的treenode。

http://www.jidesoft.com/javadoc/com/jidesoft/tree/TreeUtils.html