Java JTree只扩展一级节点

时间:2009-02-04 09:10:58

标签: java swing jtree

使用JTree,假设根节点为0级并且根目录下可能有多达5个级别,我如何轻松扩展所有1级节点,以便所有1级和1级节点都可以。可以看到2个分支和叶子,但3级和3级以下不是?

5 个答案:

答案 0 :(得分:21)

感谢快速回复的人。但是我现在找到了我正在寻找的简单解决方案。出于某种原因,我在JavaDocs中看不到DefaultMutableTreeNode.getLevel()!仅供参考我现在正在做的事情是:

    DefaultMutableTreeNode currentNode = treeTop.getNextNode();
    do {
       if (currentNode.getLevel()==1) 
            myTree.expandPath(new TreePath(currentNode.getPath()));
       currentNode = currentNode.getNextNode();
       }
    while (currentNode != null);

答案 1 :(得分:3)

你有一些Tree实用程序类正是这样做的:

this one

public class SimpleNavigatorTreeUtil {

   /**
    * Expands/Collapse specified tree to a certain level.
    * 
    * @param tree jtree to expand to a certain level
    * @param level the level of expansion
    */
   public static void expandOrCollapsToLevel(JTree tree, TreePath treePath,int level,boolean expand) {
      try {
         expandOrCollapsePath(tree,treePath,level,0,expand);
      }catch(Exception e) {
e.printStackTrace();
         //do nothing
      }
   }

   public static void expandOrCollapsePath (JTree tree,TreePath treePath,int level,int currentLevel,boolean expand) {
//      System.err.println("Exp level "+currentLevel+", exp="+expand);
      if (expand && level<=currentLevel && level>0) return;

      TreeNode treeNode = ( TreeNode ) treePath.getLastPathComponent();
      TreeModel treeModel=tree.getModel();
      if ( treeModel.getChildCount(treeNode) >= 0 ) {
         for ( int i = 0; i < treeModel.getChildCount(treeNode); i++  ) {
            TreeNode n = ( TreeNode )treeModel.getChild(treeNode, i);
            TreePath path = treePath.pathByAddingChild( n );
            expandOrCollapsePath(tree,path,level,currentLevel+1,expand);
         }
         if (!expand && currentLevel<level) return;
      }      
      if (expand) {
         tree.expandPath( treePath );
//         System.err.println("Path expanded at level "+currentLevel+"-"+treePath);
      } else {
         tree.collapsePath(treePath);
//         System.err.println("Path collapsed at level "+currentLevel+"-"+treePath);
      }
   }


}

基本上,您需要探索子节点,直到满足您的条件(此处为深度级别),然后展开所有节点直到该点。

答案 2 :(得分:1)

由于您的1级节点都是根节点的子节点,并且假设您正在使用DefaultMutableTreeNode(调用getPath()所需),您可以像根节点的子节点一样迭代这样:

Enumeration<?> topLevelNodes
    = ((TreeNode)tree.getModel().getRoot()).children();
while(topLevelNodes.hasMoreElements()) {
    DefaultMutableTreeNode node
        = (DefaultMutableTreeNode)topLevelNodes.nextElement();
    tree.expandPath(new TreePath(node.getPath()));
}

答案 3 :(得分:0)

对所有2级节点使用expand(TreePath)

答案 4 :(得分:0)

这应该有效 -

import javax.swing.*;
import javax.swing.tree.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.*;

public class Tree {
    public static void main(String[] args) {
        JPanel panel = new JPanel(new BorderLayout());
        final JTree tree = new JTree();
        panel.add(new JScrollPane(tree));
        JButton btn = new JButton("Press Me");
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                for (Enumeration e = ((TreeNode)tree.getModel().getRoot()).children();e.hasMoreElements();) {
                    TreeNode tn = (TreeNode)e.nextElement();
                    tree.expandPath(new TreePath(((DefaultTreeModel)tree.getModel()).getPathToRoot(tn)));
                }
            }
        });
        panel.add(btn, BorderLayout.SOUTH);
        JFrame frame = new JFrame("");
        frame.getContentPane().add(panel);
        frame.setSize(300, 300);
        frame.setLocation(100, 100);
        frame.pack();
        frame.show();
    }
}