Solved

Dynamically Update Elements in  JTree

Posted on 2010-11-16
7
463 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

776 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