Solved

Dynamically Update Elements in  JTree

Posted on 2010-11-16
7
461 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

743 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

12 Experts available now in Live!

Get 1:1 Help Now