Results 1 to 15 of 15
  1. #1
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Jtree update all parents when I have change childs

    Exists some effective way how to update all parents of node which was changed?

    I have JTree with following structure and when I change e.g.nodeA1 so I want to change nodeA and root. Probably it will be for this some recursive method but it fails to me. :-/ Thanks.


    Java Code:
    root
      nodeA
        nodeA1
        nodeA2
      nodeB
        nodeB1
        nodeB2

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Jtree update all parents when I have change childs

    What do you want to change? The answer is of course "yes" but you need to be more specific about what you want to achieve. Apparently you already tried a recursive way, share the code that is not working for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Jtree update all parents when I have change childs

    Quote Originally Posted by gimbal2 View Post
    ... share the code that is not working for you.
    In the form of a SSCCE (Short, Self Contained, Correct (Compilable), Example).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    Ok. Here is my code. Unfortunatelly first problem is that I can't insert children into my tree or aren't visible. In my case i don't se A1 in tree. I did some changes and it doesn't work now. After this I explain main problem of this topic. Thank you very much.

    Java Code:
     
    import java.awt.Component;
    
    import javax.swing.JFrame;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    
    public class TreeExample extends JFrame
    {
        private JTree tree;
        
        public TreeExample() {
        	tree = new JTree();
        	DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new MyNode("RootName"));
    		DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    		tree.setModel(treeModel);
    		tree.setCellRenderer(new MyNodeRenderer());
        	
    		MyNode nodeA = new MyNode("A");
    		addNode(rootNode, nodeA);
    
    		MyNode nodeB = new MyNode("B");
    		addNode(rootNode, nodeB);
    		
    		MyNode nodeA1 = new MyNode("A1");
    		DefaultMutableTreeNode nodeAroot = new DefaultMutableTreeNode(nodeA);
    		addNode(nodeAroot, nodeA1);
    		
            add(tree);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("JTree Example");       
            this.pack();
            this.setVisible(true);
        }
        
        private void addNode(DefaultMutableTreeNode parentnode, MyNode newnode) {
    		DefaultMutableTreeNode node = new DefaultMutableTreeNode(newnode);
    		DefaultTreeModel treeModel = (DefaultTreeModel) this.tree.getModel(); // get our model
    		int index = treeModel.getChildCount(parentnode); // how many children are there already?
    		treeModel.insertNodeInto(node, parentnode, index); // Adding last child
    		TreePath tp = new TreePath(parentnode.getPath());
    		this.tree.expandPath(tp);
    	}
         
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TreeExample();
                }
            });
        }     
        
        public class MyNode {
    
        	private int		count;
    
        	private String	name;
    
        	public MyNode(String name) {
        		this.name = name;
        	}
    
        	public int getCount() {
        		return count;
        	}
    
        	public void setCount(int count) {
        		this.count = count;
        	}
    
        	public String getName() {
        		return name;
        	}
    
        	public void setName(String name) {
        		this.name = name;
        	}
    
        	public String toString() {
        		return this.name;
        	}
    
        	public boolean equals(String name) {
        		return this.name.equals(name);
        	}
        }
        
        public class MyNodeRenderer extends DefaultTreeCellRenderer {
    
        	public MyNodeRenderer() {
        		setOpaque(true);
        	}
    
        	@Override
        	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        		super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        		
        		MyNode node = (MyNode) (((DefaultMutableTreeNode) value).getUserObject());
        		return this;
        	}
        }
    }

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Jtree update all parents when I have change childs

    You are using two different instances of the DefaultMutableTreeNode for A. At line 29 you create a new one, but that doesn't exist in the tree. The can't find it for insert the new node as parent. I modified your code to return the newly inserted DefaultMutableTreeNode, and that works:

    Java Code:
    public class TreeExample extends JFrame {
        private JTree tree;
    
        public TreeExample() {
            tree = new JTree();
            DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new MyNode("RootName"));
            DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
            tree.setModel(treeModel);
            tree.setCellRenderer(new MyNodeRenderer());
    
            MyNode nodeA = new MyNode("A");
            DefaultMutableTreeNode dmtnA = addNode(rootNode, nodeA); //<-------------------- a reference to the newly added node is kept here
    
            MyNode nodeB = new MyNode("B");
            addNode(rootNode, nodeB);
    
            MyNode nodeA1 = new MyNode("A1");
            addNode(dmtnA, nodeA1); //<----------------------------------- use the reference to A as parent
    
            add( new JScrollPane(tree));
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("JTree Example");
            this.setSize(400,400);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
    
        private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parentnode, MyNode newnode) { //<-------------- note the return type DefaultMutableTreeNode instead of void
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(newnode);
            DefaultTreeModel treeModel = (DefaultTreeModel) this.tree.getModel(); // get our model
            int index = treeModel.getChildCount(parentnode); // how many children are there already?
            treeModel.insertNodeInto(node, parentnode, index); // Adding last child
            TreePath tp = new TreePath(parentnode.getPath());
            this.tree.expandPath(tp);
            return node; //<-------------------------------- Now returns the created node.
        }
    
    
    <snip> the rest didn't change </snip>
    
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    Super. And now I have problem if I need update all parents if my condition is success. In my case is condition if(nodeA2.getName().contains("2"))

    Java Code:
    import java.awt.Component;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTree;
    import javax.swing.SwingUtilities;
    import javax.swing.tree.DefaultMutableTreeNode;
    import javax.swing.tree.DefaultTreeCellRenderer;
    import javax.swing.tree.DefaultTreeModel;
    import javax.swing.tree.TreePath;
    
    public class TreeExample extends JFrame
    {
        private JTree tree;
        
        public TreeExample() {
        	tree = new JTree();
        	DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(new MyNode("RootName"));
    		DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
    		tree.setModel(treeModel);
    		tree.setCellRenderer(new MyNodeRenderer());
        	
    		MyNode nodeA = new MyNode("A");
            DefaultMutableTreeNode dmtnA = addNode(rootNode, nodeA); //<-------------------- a reference to the newly added node is kept here
     
            MyNode nodeB = new MyNode("B");
            addNode(rootNode, nodeB);
     
            MyNode nodeA1 = new MyNode("A1");
            addNode(dmtnA, nodeA1); //<----------------------------------- use the reference to A as parent
    
            MyNode nodeA2 = new MyNode("A2");
            addNode(dmtnA, nodeA2); //<----------------------------------- use the reference to A as parent
     
            if(nodeA2.getName().contains("2")) {
            	// updateAllParent();
            	// IF CONDITIONS OK SO IN MY CASE I NEED UPDATE ALL PARENT IN THIS BRANCHE ... A and RootName
            	// I need increment value count in MyNode .... nodeA.setCount(nodeA.getCount()+1);
            }
            
            add( new JScrollPane(tree));
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setTitle("JTree Example");
            this.setSize(400,400);
            this.setLocationRelativeTo(null);
            this.setVisible(true);
        }
     
        private DefaultMutableTreeNode addNode(DefaultMutableTreeNode parentnode, MyNode newnode) { //<-------------- note the return type DefaultMutableTreeNode instead of void
            DefaultMutableTreeNode node = new DefaultMutableTreeNode(newnode);
            DefaultTreeModel treeModel = (DefaultTreeModel) this.tree.getModel(); // get our model
            int index = treeModel.getChildCount(parentnode); // how many children are there already?
            treeModel.insertNodeInto(node, parentnode, index); // Adding last child
            TreePath tp = new TreePath(parentnode.getPath());
            this.tree.expandPath(tp);
            return node; //<-------------------------------- Now returns the created node.
        }
         
        public static void main(String[] args)
        {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new TreeExample();
                }
            });
        }     
        
        public class MyNode {
    
        	private int		count;
    
        	private String	name;
    
        	public MyNode(String name) {
        		this.name = name;
        	}
    
        	public int getCount() {
        		return count;
        	}
    
        	public void setCount(int count) {
        		this.count = count;
        	}
    
        	public String getName() {
        		return name;
        	}
    
        	public void setName(String name) {
        		this.name = name;
        	}
    
        	public String toString() {
        		return this.name;
        	}
    
        	public boolean equals(String name) {
        		return this.name.equals(name);
        	}
        }
        
        public class MyNodeRenderer extends DefaultTreeCellRenderer {
    
        	public MyNodeRenderer() {
        		setOpaque(true);
        	}
    
        	@Override
        	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        		super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        		
        		MyNode node = (MyNode) (((DefaultMutableTreeNode) value).getUserObject());
        		return this;
        	}
        }
    }

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Jtree update all parents when I have change childs

    It doesn't really help if you ask questions from the perspective of a technical solution you have chosen. You want to ask the question from the perspective of what you want to functionally achieve and then we can see what technical solution fits the requirement.

    I think your problem is as simple as this: Say you have a tree where a branch has numbered nodes.

    Java Code:
    A
    |-1
    |-2
    |-3
    Now if you want to add a child node under 1, then that becomes node 2 and nodes 2 and 3 basically need to be renamed to nodes 3 and 4.

    Java Code:
    A
    |-1
    -------- insert new node here
    |-2
    |-3
    Java Code:
    A
    |-1
    |-2 *new*
    |-3 --- renamed
    |-4 --- renamed

    Am I correct, or am I misunderstanding you?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    I don't understand your idea. I need solve it in my specific case because I need keep value(in my case it is "count") in each node!

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Jtree update all parents when I have change childs

    Then I am going to shut up, as it doesn't help if we can't understand each other.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Jtree update all parents when I have change childs

    Quote Originally Posted by basfot View Post
    I don't understand your idea. I need solve it in my specific case because I need keep value(in my case it is "count") in each node!
    Then you need to take you original post with your tree and show a before and after once you have changed it. Add any additional tree elements as necessary to show what you want to achieve.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    Ok. I will draw from my last post with code.
    1) I have Tree

    Java Code:
    RootName
          |-A
             |--A1
             |--A2
          |-B
    My nodes are represented class MyNode. And in my actual state has attribute "count" value "0" ... so:

    Java Code:
    RootName(count = 0)
          |-A(count = 0)
             |--A1(count = 0)
             |--A2(count = 0)
          |-B(count = 0)
    And if I add into tree some node which name will be contains characte "2" ... so I need increment his all parents of parents. So after increment I need this state:

    Java Code:
    RootName(count = 1)
          |-A(count = 1)
             |--A1(count = 0)
             |--A2(count = 1)
          |-B(count = 0)

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Jtree update all parents when I have change childs

    Quote Originally Posted by basfot View Post
    Ok. I will draw from my last post with code.
    1) I have Tree

    Java Code:
    RootName
          |-A
             |--A1
             |--A2
          |-B
    My nodes are represented class MyNode. And in my actual state has attribute "count" value "0" ... so:

    Java Code:
    RootName(count = 0)
          |-A(count = 0)
             |--A1(count = 0)
             |--A2(count = 0)
          |-B(count = 0)
    And if I add into tree some node which name will be contains characte "2" ... so I need increment his all parents of parents. So after increment I need this state:

    Java Code:
    RootName(count = 1)
          |-A(count = 1)
             |--A1(count = 0)
             |--A2(count = 1)
          |-B(count = 0)
    So I presume then, if you set count = 0 in A1, it would then look like this:

    Java Code:
    RootName(count = 2)
          |-A(count = 2)
             |--A1(count = 1)
             |--A2(count = 1)
          |-B(count = 0)
    If that is the case, can't you just get the TreePath from that node to root and update the count accordingly as you traverse the path? I am not that well versed in JTree but I presume there is some way to traverse from a node to the root.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    No. Just I add node to JTree and in dependent on condition (in my case it's name of MyNode contains String "2") I updated parent nodes. For example:

    1)Initial state:
    Java Code:
    RootName(count = 0)
          |-A(count = 0)
             |--A1(count = 0)
          |-B(count = 0)
    2) Adding A2 into A
    Java Code:
    RootName(count = 1) incremented
          |-A(count = 1) incremented
             |--A1(count = 0)
             |--A2(count = 1) incremented
          |-B(count = 0)
    3) Adding B1 into B ... without changes
    Java Code:
    RootName(count = 1) 
          |-A(count = 1) 
             |--A1(count = 0)
             |--A2(count = 1) 
          |-B(count = 0)
    4) Adding B2 into B ... without changes
    Java Code:
    RootName(count = 2) incremented
          |-A(count = 1) 
             |--A1(count = 0)
             |--A2(count = 1)
          |-B(count = 1) incremented
             |--B2(count = 1) incremented
    5) Adding A12 into A1 ... without changes
    Java Code:
    RootName(count = 3) incremented
          |-A(count = 2) incremented
             |--A1(count = 1) incremented
                  |--A12(count = 1) incremented
             |--A2(count = 1)
          |-B(count = 1)
             |--B2(count = 1)
    I can't find way how it to do.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Jtree update all parents when I have change childs

    Change your design. Extend DefaultMutableTreeNode, not Object, to add the count and name attributes.

    You can then use the getPath() method which
    Returns the path from the root, to get to this node.
    and iterate over the TreeNode[] returned, casting each node to MyNode to access and update the count.

    If that's not totally clear to you, take a stab at it anyhow and we'll take it from there.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    basfot is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Jtree update all parents when I have change childs

    Thanks. Finally I solved with TreeNode ... after adding node I call updateParents()

    Java Code:
    	private void updateParents(TreeNode rootNode) {
    		if(rootNode.getParent() != null) {
    			MyNode myNode= (MyNode ) (((DefaultMutableTreeNode) rootNode).getUserObject());
    			myNode.incrementCount();
    			updateParents(rootNode.getParent());
    		} else {
    			MyNode myNode= (MyNode ) (((DefaultMutableTreeNode) rootNode).getUserObject());
    			myNode.incrementCount();
    		}
    	}
    Last edited by basfot; 03-20-2015 at 01:20 PM.

Similar Threads

  1. how change the update images
    By mDennis10 in forum New To Java
    Replies: 7
    Last Post: 12-11-2011, 12:28 AM
  2. Update JTree
    By MediumDave in forum AWT / Swing
    Replies: 1
    Last Post: 01-09-2011, 04:24 AM
  3. How to change JTree's node icon manually?
    By LeonLanford in forum AWT / Swing
    Replies: 3
    Last Post: 09-29-2010, 03:14 PM
  4. Replies: 1
    Last Post: 07-10-2010, 04:21 AM
  5. Update the JFrame after change the Content Pane
    By alisonchan30 in forum AWT / Swing
    Replies: 1
    Last Post: 04-26-2010, 06:22 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
  •