Results 1 to 3 of 3
  1. #1
    lmsook10 is offline Member
    Join Date
    Jun 2008
    Rep Power

    Post Why my JTree always collasped after refreshing?

    I have been spending whole day trying to figure out why my JTree always collasped after refreshing, but I haven't got any fruit.

    I have a Tree and it is collasped in the beginning, and I can expand and do some work(adding nodes or deleting nodes - no problem)
    But there is a button which validates the tree, and if a node has problem, its background changes which part is working Okay.
    but the tree always goes back to collasped(original state)

    I tried every methods that I searched, but nothing is working.

    Below is the codes..

    Java Code:
    private void validateManifest() {
           	CP_EditorHandler.checkManifestErrors(_contentPackage, false);
            ManifestTree manifestTree = _manifestPanel.getManifestTree();
    	// get the selected node before updating
            ManifestTreeNode selectedNode = (ManifestTreeNode) manifestTree.getSelectedNode();
    	// get the selected path before updating
            TreePath selectedPath = (TreePath) manifestTree.getSelectionPath();
    	// below code is the part that recreates nodes of the tree and updates nodes' background
    	// I set below code to expand to selected path
    	// I even added this code to make it sure
            manifestTree.setExpandedState(selectedPath, true);
    I even added addTreeWillExpandListener in the tree constructor, but it doesn't work either.

    Java Code:
      	  (new TreeWillExpandListener() {
      	    public void treeWillExpand(TreeExpansionEvent e) { }
      	    public void treeWillCollapse(TreeExpansionEvent e)
      	         throws ExpandVetoException {
      	     throw new ExpandVetoException(e, "you can't collapse this JTree");
    What did I do wrong or what should I do to make it work?

    I really appreciate your help.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    Java Code:
    import java.awt.*;
    import java.util.Enumeration;
    import javax.swing.*;
    import javax.swing.tree.*;
    public class TreeExpansionTest {
        private JScrollPane getContent() {
            JTree tree = new JTree();
            expand(tree, tree.getPathForRow(0));
            return new JScrollPane(tree);
        private void expand(JTree tree, TreePath path) {
            TreeNode node = (TreeNode)path.getLastPathComponent();
            if (node.getChildCount() > 0) {
                Enumeration e = node.children();
                while(e.hasMoreElements()) {
                    TreeNode n = (TreeNode)e.nextElement();
                    expand(tree, path.pathByAddingChild(n));
        public static void main(String[] args) {
            JFrame f = new JFrame();
            f.add(new TreeExpansionTest().getContent());

  3. #3
    lmsook10 is offline Member
    Join Date
    Jun 2008
    Rep Power


    Now I got it work thanks to your codes sample.
    Thanks a lot!!

Similar Threads

  1. Node selection in jtree
    By Preethi in forum AWT / Swing
    Replies: 4
    Last Post: 06-19-2008, 11:25 PM
  2. Bug in refreshing jsp
    By anki1234 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 12-31-2007, 08:09 AM
  3. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 PM
  4. how to stop refreshing page
    By cecily in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 01:25 AM
  5. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts