Solved

Dynamically Update Elements in  JTree

Posted on 2010-11-16
7
462 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
  • 3
  • 3
7 Comments
 
LVL 35

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Accepted Solution

by:
mccarl earned 400 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 100 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 35

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 87
java 8 lambda expresssions exception handling 3 75
What are programming codes and how do they work? 7 75
use lov values 2 38
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now