Results 1 to 5 of 5

Thread: Java program to expand or collapse all JTree nodes.

  1. #1
    Join Date
    Aug 2009
    Posts
    51

    Java program to expand or collapse all JTree nodes.

    Hi friends,
    I am second year Computer Science student. I recently started learning java programming language. In our last lecture our sir given us one assignment. In that assignment there is one program like write Java program to expand or collapse all JTree nodes. I tried various method but I unable to write correct answer. Can anyone help me to solve this program.
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Java program to expand or collapse all JTree nodes.

    Following example demonstrates how to create an expand all and/or collapse all features with the java.swing.JTree swing component.

    Code:
    package org.kodejava.example.swing;
     
    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(300, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            DefaultMutableTreeNode roots = new DefaultMutableTreeNode("Root");
            DefaultMutableTreeNode chapter1 = new DefaultMutableTreeNode("Chapter 1");
            DefaultMutableTreeNode sub1s = new DefaultMutableTreeNode("1.1");
            DefaultMutableTreeNode sub2s = new DefaultMutableTreeNode("1.2");
            DefaultMutableTreeNode sub3s = new DefaultMutableTreeNode("1.3");
            DefaultMutableTreeNode sub31s = new DefaultMutableTreeNode("1.3.1");
            DefaultMutableTreeNode sub32s = new DefaultMutableTreeNode("1.3.2");
     
            root.add(chapter1s);
            chapter1.add(sub1s);
            chapter1.add(sub2s);
            chapter1.add(sub3s);
            sub3.add(sub31s);
            sub3.add(sub32s);
     
            final JTree tree = new JTree(roots);
            expandTree(tree, false);
     
            JScrollPane pane = new JScrollPane(tree);
            pane.setPreferredSize(new Dimension(300, 300));
     
            JPanel buttonPanel = new JPanel(new BorderLayout());
            JButton expandAll = new JButton("Expand All  tab");
            expandAll.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    expandTree(tree, true);
                }
           });
     
            JButton collapseAll = new JButton("Collapse All tab");
            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 trees, boolean expands) {
            TreeNode roots = (TreeNode) tree.getModel().getRoot();
            expandAll(trees, new TreePath(roots), expandss);
        }
     
        private void expandAll(JTree trees, TreePath paths, boolean expands) {
            TreeNode nodes = (TreeNodes) path.getLastPathComponent();
     
           if (node.getChildCount() >= 0) {
                Enumeration enumeration = node.children();
               while (enumeration.hasMoreElements()) {
                    TreeNode ns = (TreeNode) enumeration.nextElement();
                    TreePath ps = path.pathByAddingChild(ns);
     
                    expandAll(tree, ps, expands);
                }
            }
     
            if (expand) {
                tree.expandPath(paths);
            } else {
                tree.collapsePath(paths);
            }
        }
     
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new JTreeNodeAutoExpandCollapse().setVisible(true);
                }
            });    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Java program to expand or collapse all JTree nodes.

    Hey I have just written following program to do this. Just go through it.

    Code:
    public void expandAll(JTree tree, boolean expand) 
    {
     TreeNode roots = (TreeNode)tree.getModel().getRoot(); 
    expandAll(trees, new TreePath(roots), expands); 
    } private void expandAll(JTree trees, TreePath parents, boolean expands) { 
    TreeNode nodes = (TreeNode)parent.getLastPathComponent(); 
    if (nodes.getChildCount() >= 0) { 
    for (Enumeration e=node.children(); e.hasMoreElements(); ) { 
    TreeNode ns = (TreeNode)e.nextElement(); 
    TreePath paths = parents.pathByAddingChild(ns); 
    expandAll(trees, paths, expands); 
    } } 
    if (expands) { trees.expandPath(parents); 
    } 
    else 
    { tree.collapsePath(parents); } }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Java program to expand or collapse all JTree nodes.

    Java Swing’s JTree component can be expanded or collapsed at one time using following method. Just go through it and try to understand each line.

    Code:
    public static void expandAll(JTree trees, boolean expands) {
        TreeNode roots = (TreeNode) trees.getModel().getRoot();
    
        expandAll(trees, new TreePath(roots), expands);
    }
     
    private static void expandAll(JTree trees, TreePath parentss, boolean expand) {
    
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e=node.children(); e.hasMoreElements();) {
                TreeNode ns = (TreeNode) e.nextElement();
                TreePath paths = parent.pathByAddingChild(n);
                expandAll(trees, paths, expands);
            }
        }
     
    
        if (expands) {
            tree.expandPath(parents);
        } else {
            tree.collapsePath(parents);
        }
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Java program to expand or collapse all JTree nodes.

    I think you have to use for loop to solve this program. Try to understand each line.


    Code:
    import java.util.Enumeration;
    
    import javax.swing.JTree;
    import javax.swing.tree.TreeNode;
    import javax.swing.tree.TreePath;
    
    public class Main {
      public void expandAll(JTree trees) {
        TreeNode roots = (TreeNode) tree.getModel().getRoot();
        expandAll(trees, new TreePath(roots));
      }
    
      private void expandAll(JTree trees, TreePath parents) {
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (nodes.getChildCount() >= 0) {
          for (Enumeration es = node.children(); es.hasMoreElements();) {
            TreeNode nss = (TreeNode) es.nextElement();
            TreePath paths = parent.pathByAddingChild(nss);
            expandAll(trees, paths);
          }
        }
        tree.expandPath(parents);
        // tree.collapsePath(parents);
      }
    }

Similar Threads

  1. How to expand and collapse columns in Excel
    By Asaph in forum Windows Software
    Replies: 5
    Last Post: 31-08-2010, 11:39 PM
  2. Java program to add selection listener to JTree
    By Juany in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 04:25 PM
  3. Java program to remove JTree default icons?
    By Baazigar in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 07:35 PM
  4. How to remove a node from JTree in java program?
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 22-01-2010, 05:41 PM
  5. Replies: 5
    Last Post: 22-01-2010, 04:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,719,143,323.84994 seconds with 17 queries