Link to home
Create AccountLog in
Avatar of damixa
damixaFlag for Denmark

asked on

Dynamically Update Elements in JTree

I have a tree let say
A
   B
      C
      D
   E
      F

When I click on f.e B in another Panel I see the value of B in a text field, what I need to know
if there is a way that once I change the value in text field for B, it will automatically change the value for B in the tree.
Im not sure that there is any update function for Jtrees
thanks
Avatar of mccarl
mccarl
Flag of Australia image

How are you setting up the JTree at the moment? Can you post the code that you have?
Avatar of damixa

ASKER

Part of the code as this is a big app.
but i extracted the code that is related to the tree.
               DefaultMutableTreeNode robotsNode = getRobotTree();
            //model = new XMLTreeModel();
            tree = new JTree(model);            
            model = new DefaultTreeModel(robotsNode);  
******************
            tree.setModel(model);
            tree.setShowsRootHandles(true);
            tree.setEditable(false);
*************************************
        
            tree.addTreeSelectionListener(new TreeSelectionListener() {
                    public void valueChanged(TreeSelectionEvent e) {

                    DefaultMutableTreeNode selectedNode = getSelectedNode();
                    if (selectedNode!=null) {
                    if (getNodeType(selectedNode)=="Root" ) {
                        addShapeButton.setEnabled(false);
                          addComponentButton.setEnabled(true);
                  }
                    else {
                          addComponentButton.setEnabled(false);      
                          addShapeButton.setEnabled(true);}
                    String View;
                    System.out.println("Clicked;" + getNodeName(selectedNode));
                    if (getNodeType(selectedNode)=="Root")
                          View="  Root  ";
                    else
                          View= getNodeName(selectedNode);
                    tFields.setText(View);
              
                 }}
              });       




       private static String getNodeName(DefaultMutableTreeNode TNode) {
             String nodeString=" ";      
             String[] tmp= TNode.getUserObject().toString().split(" ");
             if (tmp.length>1)
             nodeString=tmp[1];
             return nodeString;
       }
        private static DefaultMutableTreeNode getSelectedNode() {
                return (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
              }

              private static DefaultMutableTreeNode getRobotTree() {
                    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Root");
                return rootNode;
              }
Avatar of damixa

ASKER

Now what I want to do that when i change the value in the text field tField the value is directly changed in the tree.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Also, you will have to call repaint() on the tree after you update the node's userObject
Avatar of damixa

ASKER

Thanks, I used just an actionperformed and worked fine, as I have no experience with document listeners. Well If you objects If you have a simple document listener example I can open another question so you can get other points. for this one I will give most of the points to Mccarl. hope it is ok for you.