Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamically Update Elements in  JTree

Posted on 2010-11-16
7
Medium Priority
?
470 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 34152125
How are you setting up the JTree at the moment? Can you post the code that you have?
0
 

Author Comment

by:damixa
ID: 34152177
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;
              }
0
 

Author Comment

by:damixa
ID: 34152191
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.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 36

Accepted Solution

by:
mccarl earned 1600 total points
ID: 34152216
All you should have to do is keep track of the Node that is being edited (i.e. store this away in the TreeSelectionListener) and then when the value in the textField changes, either by have a listener on that or on a button that "Saves" the value, and then setting the userobject on the Node that you have stored away to the new text in the textField.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 34152246
you want to add a DocumentListener to the text field that updates the tree model whenever it is notified of a change in the text field
0
 
LVL 36

Expert Comment

by:mccarl
ID: 34152253
Also, you will have to call repaint() on the tree after you update the node's userObject
0
 

Author Comment

by:damixa
ID: 34152316
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
In this post we will learn different types of Android Layout and some basics of an Android App.
Six Sigma Control Plans
Progress
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question