Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

giving action to tree nodes

I have a Java applciation that retreives data from the Oracle database in a tree structure. Basically the tree has three nodes, lets say A, B, C. SO when the user selects node B or C and clicks the action button, I want to retreive the related data in an xml format and send the data into an other table. I am able to retreive data in xml format and also send this data but I am unable to set action for particular node. I am writing a method that identifies the node that is being selected and later want to write a code for sending the retreived data. So can somebody tell me or give some code snippet of how to make the prg identify the selected node and give related action to it.
All contributions will be greatly appreciatted.

my method looks like this

----

private void insertXML()
      {
      jTree1.addTreeSelectionListener(new TreeSelectionListener() {
      public void valueChanged(TreeSelectionEvent evt) {
        TreePath[] paths = evt.getPaths();
                                  
       for (int i=0; i<paths.length; i++) {
        if (evt.isAddedPath(i)) {
       
                                System.out.println(i+""+"s selected");
                                                   }
                       else {

                               }
                                                                                 }
                                                                       }
                                                                             });


-----------

tree node looks like this, so basically i want to implement action on 2nd node ie Version and 3rd node UA

            DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode(rset1.getString("ang_nr1"));
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode(rset1.getString("ver_nr1"));
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode(rset1.getString("faktor1"));
              Version.add(UA);

----------

Mainly what I want to achieve is  that on selection of a particular node, I want to send XML data.......
0
vihar123
Asked:
vihar123
  • 14
  • 8
1 Solution
 
zzynxSoftware engineerCommented:
jTree1.addTreeSelectionListener(new TreeSelectionListener() {
     public void valueChanged(TreeSelectionEvent evt) {
         DefaultMutableTreeNode nodeSelected = jTree1.getSelectionPath().getLastPathComponent();
         System.out.println(nodeSelected.toString() + " is selected");
     }
}
0
 
zzynxSoftware engineerCommented:
It should print out:

"xxx is selected" with

xxx being
               rset1.getString("ang_nr1")
       or
               rset1.getString("ver_nr1")
       or
               rset1.getString("faktor1")
0
 
vihar123Author Commented:
its giving me an error as incompatible types

------


private void insertXML()
                              {
                                 jTree1.addTreeSelectionListener(new TreeSelectionListener() {
                                   public void valueChanged(TreeSelectionEvent evt) {
                           DefaultMutableTreeNode nodeSelected = jTree1.getSelectionPath().getLastPathComponent();
                           System.out.println(nodeSelected.toString() + " is selected");
                       }
                                                                                                                              });
                              }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zzynxSoftware engineerCommented:
Of course, you have to cast it:

DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode) jTree1.getSelectionPath().getLastPathComponent();
0
 
zzynxSoftware engineerCommented:
getLastPathComponent() just returns an Object (see documentation) while we need a DefaultMutableTreeNode
0
 
vihar123Author Commented:
its displaying the results double times when im selecting an other node

----

10 is selected
2 is selected
2 is selected
10 is selected
10 is selected
10 is selected
0
 
zzynxSoftware engineerCommented:
I think that's the deselecting event of the previous. Have to filter that out somehow...
0
 
zzynxSoftware engineerCommented:
Can you comment this output with the actions you performed:

E.g.
---- I selected node 10 ------
10 is selected
---- I selected node 2 -------
2 is selected
2 is selected
--- I selected node 10 ------
10 is selected
10 is selected
-- ??? -----
10 is selected

Was it like this?
0
 
zzynxSoftware engineerCommented:
>> its displaying the results double times when im selecting an other node
Are you sure?
I can't simulate that behaviour...
0
 
vihar123Author Commented:
>>>Can you comment this output with the actions you performed:

when im selecting the first node, its giving the output only once, when im selecting the second node, its giving me two times, like wise third time selected node, then three times.

well this output is not important, what i want to achieve is that it recognises the node being selected by the user so that i can implement action for eg:-

                   emp name
                         |
                          emp number
                                |
                                  department number

so when the user selects emp number or the department number, then it shoud recognise that it belong to the emp name and then it bring out some info relating to that particular emp name.


0
 
zzynxSoftware engineerCommented:
Well, you have the selected tree node:

    DefaultMutableTreeNode nodeSelected = jTree1.getSelectionPath().getLastPathComponent();

And you can always get the parent node:

    DefaultMutableTreeNode parentNode = nodeSelected.getParent();

So, I guess that's all your really need.


Remark: for the moment you store a String as user object in the DefaultMutableTreeNode:
>> new DefaultMutableTreeNode(rset1.getString("ang_nr1"));

I would store some Object that can - besides its name also - return you it's type: emp name, emp number or department number.

That way, you can write

              DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode( new YourObject(YourObject.EMP_NAME, rset1.getString("ang_nr1")) );
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode( new YourObject(YourObject.EMP_NUMBER, rset1.getString("ver_nr1")) );
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode( new YourObject(YourObject.DEP_NUMBER, rset1.getString("faktor1")) );
              Version.add(UA);

instead of

>>              DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode(rset1.getString("ang_nr1"));
>>              topnode.add(Angnr);              
>>              DefaultMutableTreeNode Version = new DefaultMutableTreeNode(rset1.getString("ver_nr1"));
>>              Angnr.add(Version);
>>              DefaultMutableTreeNode UA = new DefaultMutableTreeNode(rset1.getString("faktor1"));
>>              Version.add(UA);

And the you can:

        DefaultMutableTreeNode nodeSelected = jTree1.getSelectionPath().getLastPathComponent(); // the selected node
        YourObject obj = (YourObject) nodeSelected.getUserObject();
        if ( obj.getType() == YourObject.EMP_NAME )       // selected node = an emp name node
           // Nothing to do
        else if ( obj.getType() == YourObject.EMP_NUMBER ) {  // selected node is an emp number node
            DefaultMutableTreeNode parentNode = nodeSelected.getParent(); // get it's parent node
            obj = (YourObject)parentNode.getUserObject();
            // obj.getName() then returns the emp name you need
        }
        else if ( obj.getType() == YourObject.DEP_NUMBER ) {
            DefaultMutableTreeNode parentNode = nodeSelected.getParent().getParent(); // get the parent node of its parent node
            obj = (YourObject)parentNode.getUserObject();
            // obj.getName() then returns the emp name you need
        }
0
 
zzynxSoftware engineerCommented:
with of course:

public class YourObject {

    public final int EMP_NAME = 1;
    public final int EMP_NUMBER = 2;
    public final int DEP_NUMBER = 3;

    private int type;
    private String name;

    public YourObject(int type, String name) {
        this.type = type;
        this.name = name;
    }

    public int getType() { return type; }
    public String getName() { return name; }
}
0
 
vihar123Author Commented:
>>> i didnt understand with the yourObject, actully the tree is calling data from the database, so each node has its own value, in my prg

     angnr
        |
         ver number
               |
                UA number
      angnr
          |
           ver number
               |
                UA number

so when the user selects ver number or the ua number, it shld identify that it belongs to that particular angnr.

well my code looks like this...

----------

try
        {
            DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
           
            conn1 =
            DriverManager.getConnection ("");
           
            stmt1 = conn1.createStatement ();
                rset1 = stmt1.executeQuery("select * from regdet");
                
                           while (rset1.next())
               {
                       
                DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode(rset1.getString("ang_nr1"));
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode(rset1.getString("ver_nr1"));
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode(rset1.getString("faktor1"));
              Version.add(UA);
           
              }
                              conn1.close();
                              stmt1.close();                                                      
                              rset1.close();
                              
                    jTree1.setRootVisible(true);
                    treeModel.reload();      
                   
                     
        }
                 
        catch (SQLException e)
        {
            System.out.println(e);
        }
       
                         /**** Tree Action *****/
          jTree1.addTreeSelectionListener(new TreeSelectionListener() {
              
                public void valueChanged(TreeSelectionEvent evt) {
                                                                                       
                                 TreePath tp = evt.getNewLeadSelectionPath();
                             Connection conn2;
                               Statement stmt2;
                               ResultSet rset2 = null;
                               DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) tp.getLastPathComponent();                               
                               String empNo = node1.toString();
             
                                                              
                               try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn2 = DriverManager.getConnection ("");
           
                              stmt2 = conn2.createStatement ();
                rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");
                                  
                               
                               while (rset2.next())
                           {
                            jTextField1.setText(node1.toString());
                            jTextField2.setText(rset2.getString("ang_nr1"));
                            jTextField3.setText(rset2.getString("prod_bez1"));
                            jTextField4.setText(rset2.getString("kunde_nr1"));
                            jTextField5.setText(rset2.getString("ref_nr1"));
                            jTextField6.setText(rset2.getString("proj_nr1"));
                            jTextField7.setText(rset2.getString("basisprojekt1"));
                            
                           }
                              
                                          conn2.close();
                                          stmt2.close();
                                          rset2.close();
                                     
                                  }                
                 
                 
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                                                             
                             Connection conn3;
                               Statement stmt3;
                               ResultSet rset3 = null;    
                   DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) tp.getLastPathComponent();                                            
                               String verNo = node2.toString();                               
                               
                   try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn3 = DriverManager.getConnection ("");
           
                              stmt3 = conn3.createStatement ();
                      rset3 = stmt3.executeQuery("select * from regdet WHERE ver_nr1 = '" + verNo + "'");
                                        
                               clearForm();      
                                              
                               while (rset3.next())
                           {
                            jTextField8.setText(rset3.getString("ver_nr1"));
                                    jTextField9.setText(rset3.getString("gesc_jahr1"));
                                    jTextField10.setText(rset3.getString("ver_besh1"));
                                    jTextField11.setText(rset3.getString("curr1"));
                                    jTextField12.setText(rset3.getString("ges_kalk1"));
                                    jTextField13.setText(rset3.getString("kalk_ele1"));
                                    jTextField14.setText(rset3.getString("kalk_mek1"));
                               }
                              
                                          conn3.close();
                                          stmt3.close();
                                          rset3.close();      
                                    
                                  }
                          
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                                           
                         DefaultMutableTreeNode nodesel = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
                           if (nodesel == null) return;
                           Object nodeInfo = nodesel.getUserObject();
                          
                           if (nodesel.isLeaf()) {
                              
                               System.out.println(nodesel);
                   
                           }
                   
                     else {
                   
                            }
                               
                               
                                             
                          
                                                                                        }
               
                                                                                                    }
       
                                                          );      


0
 
vihar123Author Commented:
i think its little bit confusing, well ill try to explain in detail, as u can see from my above code that i have designed tree with three nodes and calling data from the database table, so now i want to implement action in such a way that when verno or the ua number is selected, it shld check to which angnr it belongs and when user cliks action button take the data belonging to that particualr angnr and send it to an other table.

this is what im trying to achieve......hope its clear now :-).

thanks.......
0
 
zzynxSoftware engineerCommented:
>> i didnt understand with the yourObject
Regrettable

>>actully the tree is calling data from the database, so each node has its own value
I do understand that perfectly


Thing that is rather unclear to me is

>>emp name
>>       |
>>   emp number
>>           |
>>       department number

and

>>      angnr
>>          |
>>        ver number
>>               |
>>             UA number

Which make me think that

emp name                        corresponds with              angnr
emp number                                                              ver number
department number                                                  UA number

But further on you write
>>  rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");

Which looks like
emp ***number***                        corresponds with              angnr



>> well my code looks like this
Does it work?
It isn't optimal for sure.


You just take the selected node
>> DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) tp.getLastPathComponent();
and *assume* that it is an emp Number node (a ver number node)
>> String empNo = node1.toString();

with that empNo you **try** the query

>> rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");

then you ***assume*** that it is a ver number (confusion as told above again)
>> String verNo = node2.toString()
since you **try*** the query
>>rset3 = stmt3.executeQuery("select * from regdet WHERE ver_nr1 = '" + verNo + "'");


With my YourObject proposal I was trying to tell you that the selected node can tell you ***exactly*** what type it is:
you don't have to ***assume*** nor ***try***.


To conclude: Do you still have questions left? Or are you happy with your own code?
0
 
vihar123Author Commented:
well sorry i just used that emp thing to explain, so please lets forget about it, i do need some help, and this time ill put the entire question again and what i want to achieve

so just forget the whole thing i have written above, and please excuse me.

Question:-

I have a tree with three nodes Angnr, Version and UA.
              DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode(rset1.getString("ang_nr1"));
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode(rset1.getString("ver_nr1"));
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode(rset1.getString("faktor1"));
              Version.add(UA);

Each node calls data from the database table and populates it.
        rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");
       rset3 = stmt3.executeQuery("select * from regdet WHERE ver_nr1 = '" + verNo + "'");
>>>so empNo and the verNo are the columns from the regdet tablle

So what i want to do now is that when the user selects the verNo or the UA, it shld recognise to which angnr it belongs and then get data from the db and send it to an other table.

so for eg:-

1001(angnr)
        |
         10(ver number)
               |
               20( UA number)
1002(angnr)
       |
       10(ver number)
               |
                30(UA number)

when user selects ver number 10 belonging to 1001, then it shld bring out some data belonging to it and send it into an other table.
So could you tell me how to go about it.

thanks

0
 
zzynxSoftware engineerCommented:
Consider the class

public class YourObject {

    public final int ANGNR = 1;
    public final int VERNR = 2;
    public final int UANR = 3;

    private int type;  // one of the above
    private String number;  // the number in String format

    public YourObject(int type, String number) {
        this.type = type;
        this.number = number;
    }

    public int getType() { return type; }
    public String getNumber() { return number; }
}

Then build up your tree like

              DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode( new YourObject(YourObject.ANGNR, rset1.getString("ang_nr1")) );
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode( new YourObject(YourObject.VERNR, rset1.getString("ver_nr1")) );
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode( new YourObject(YourObject.UANR, rset1.getString("faktor1")) );
              Version.add(UA);

>> So what i want to do now is that when the user selects the verNo or the UA, it shld recognise to which angnr it belongs

        DefaultMutableTreeNode nodeSelected = jTree1.getSelectionPath().getLastPathComponent(); // the selected node
        YourObject obj = (YourObject) nodeSelected.getUserObject();
        String angNumber = null;
        if ( obj.getType() == YourObject.ANGNR ) {      // 1) the selected node is an angnr node
           angNumber = obj.getNumber();
        }
        else if ( obj.getType() == YourObject.VERNR ) {  // 2) the selected node is an version number node
            DefaultMutableTreeNode parentNode = nodeSelected.getParent(); // get it's parent node (the angnr node)
            obj = (YourObject)parentNode.getUserObject();
            angNumber = obj.getNumber();  // this is the angnr the selected vernr node belongs to
        }
        else if ( obj.getType() == YourObject.UANR ) {
            DefaultMutableTreeNode parentNode = nodeSelected.getParent().getParent(); // get the parent node of its parent node
            obj = (YourObject)parentNode.getUserObject();
            angNumber = obj.getNumber();  // this is the angnr the selected ua nr node belongs to
        }

        // Now, here you can work with the String angNumber like e.g.
        rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + angNumber + "'");

I hope you do understand it now. I can't make it any clearer.
Simply try the code...
0
 
zzynxSoftware engineerCommented:
Sorry I forgot some (DefaultMutableTreeNode) castings again:

        DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent(); // the selected node
        YourObject obj = (YourObject) nodeSelected.getUserObject();
        String angNumber = null;
        if ( obj.getType() == YourObject.ANGNR ) {      // 1) the selected node is an angnr node
           angNumber = obj.getNumber();
        }
        else if ( obj.getType() == YourObject.VERNR ) {  // 2) the selected node is an version number node
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) nodeSelected.getParent(); // get it's parent node (the angnr node)
            obj = (YourObject)parentNode.getUserObject();
            angNumber = obj.getNumber();  // this is the angnr the selected vernr node belongs to
        }
        else if ( obj.getType() == YourObject.UANR ) {
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) nodeSelected.getParent().getParent(); // get the parent node of its parent node
            obj = (YourObject)parentNode.getUserObject();
            angNumber = obj.getNumber();  // this is the angnr the selected ua nr node belongs to
        }
0
 
vihar123Author Commented:
>>>> the whole tree changed into this format......its no more showing angnr, versions, ua


treeSelectionObject@171f189
                    |
                    treeSelectionObject@171f189
                                     |
                                  treeSelectionObject@171f189

treeSelectionObject@17cec96
                    |
                  treeSelectionObject@17cec96
                                    |
                                 treeSelectionObject@17cec96
------------------------
here is the new code


try
        {
            DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
           
            conn1 =
            DriverManager.getConnection ("");
           
            stmt1 = conn1.createStatement ();
                rset1 = stmt1.executeQuery("select * from regdet");
                
                           while (rset1.next())
               {
                       
                DefaultMutableTreeNode Angnr = new DefaultMutableTreeNode( new treeSelectionObject(treeSelectionObject.ANGNR, rset1.getString("ang_nr1")) );
              topnode.add(Angnr);              
              DefaultMutableTreeNode Version = new DefaultMutableTreeNode( new treeSelectionObject(treeSelectionObject.VERNR, rset1.getString("ang_nr1")) );
              Angnr.add(Version);
              DefaultMutableTreeNode UA = new DefaultMutableTreeNode( new treeSelectionObject(treeSelectionObject.UANR, rset1.getString("ang_nr1")) );
              Version.add(UA);
           
              }
                              conn1.close();
                              stmt1.close();                                                      
                              rset1.close();
                              
                    jTree1.setRootVisible(true);
                    treeModel.reload();      
                   
                     
        }
                 
        catch (SQLException e)
        {
            System.out.println(e);
        }
       
                         /**** Tree Action *****/
          jTree1.addTreeSelectionListener(new TreeSelectionListener() {
              
                public void valueChanged(TreeSelectionEvent evt) {
                                                                                       
                         TreePath tp = evt.getNewLeadSelectionPath();
                             Connection conn2;
                               Statement stmt2;
                               ResultSet rset2 = null;
                               DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) tp.getLastPathComponent();                               
                               String empNo = node1.toString();
             
                                                              
                               try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn2 = DriverManager.getConnection ("");
           
                              stmt2 = conn2.createStatement ();
                                  rset2 = stmt2.executeQuery("select * from regdet WHERE ang_nr1 = '" + empNo + "'");
                                  
                               
                               while (rset2.next())
                           {
                            jTextField1.setText(node1.toString());
                            jTextField2.setText(rset2.getString("ang_nr1"));
                            jTextField3.setText(rset2.getString("prod_bez1"));
                            jTextField4.setText(rset2.getString("kunde_nr1"));
                            jTextField5.setText(rset2.getString("ref_nr1"));
                            jTextField6.setText(rset2.getString("proj_nr1"));
                            jTextField7.setText(rset2.getString("basisprojekt1"));
                            
                           }
                              
                                          conn2.close();
                                          stmt2.close();
                                          rset2.close();
                                     
                                  }                
                 
                 
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                                                             
                             Connection conn3;
                               Statement stmt3;
                               ResultSet rset3 = null;    
                               DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) tp.getLastPathComponent();                                            
                               String verNo = node2.toString();                               
                               
                   try
                                {
                              DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());
                     
                        conn3 = DriverManager.getConnection ("");
           
                              stmt3 = conn3.createStatement ();
                      rset3 = stmt3.executeQuery("select * from regdet WHERE ver_nr1 = '" + verNo + "'");
                                        
                               clearForm();      
                                              
                               while (rset3.next())
                           {
                            jTextField8.setText(rset3.getString("ver_nr1"));
                                    jTextField9.setText(rset3.getString("gesc_jahr1"));
                                    jTextField10.setText(rset3.getString("ver_besh1"));
                                    jTextField11.setText(rset3.getString("curr1"));
                                    jTextField12.setText(rset3.getString("ges_kalk1"));
                                    jTextField13.setText(rset3.getString("kalk_ele1"));
                                    jTextField14.setText(rset3.getString("kalk_mek1"));
                               }
                              
                                          conn3.close();
                                          stmt3.close();
                                          rset3.close();      
                                    
                                  }
                          
                          catch (SQLException e)
                                {
                                    System.out.println(e);
                                }
                                           
                                  //getting the selected node and giving action to it
                                  
                                  DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent();
                                  treeSelectionObject obj = (treeSelectionObject) nodeSelected.getUserObject();
                                  
                                  String angNumber = null;
                                           
                                           if ( obj.getType() == treeSelectionObject.ANGNR ) {    
                                       angNumber = obj.getNumber();
                                       System.out.println(obj);
                                                                                                            
                                                                                                             }
                                                else if ( obj.getType() == treeSelectionObject.VERNR ) {  
                                                // 2) the selected node is an version number node
                                          DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)nodeSelected.getParent();
                          obj = (treeSelectionObject)parentNode.getUserObject();
                                          angNumber = obj.getNumber();
                                                                        System.out.println(obj);
                                                                                                                          }
                                           
                                           else if ( obj.getType() == treeSelectionObject.UANR ) {
                                     DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)nodeSelected.getParent().getParent();
                               obj = (treeSelectionObject)parentNode.getUserObject();
                                    angNumber = obj.getNumber();  
                                                                  System.out.println(obj);
                                                                                                          }

      
                                        
                                        
                          
                                                                                        }
               
                                                                                                    }
       
                                                          );      
            
    }  

0
 
zzynxSoftware engineerCommented:
>>>> the whole tree changed into this format......its no more showing angnr, versions, ua
I forgot you also need to have a toString() function in your treeSelectionObject:

add

      public String toString() {
           return getNumber();
      }

and it will show you the numbers again.

BTW. It's common use to give classes names that start with an upper case. (TreeSelectionNode)
         Variables and functions start with a lowercase.
0
 
vihar123Author Commented:
thanks its working

well ill give this below one as a new question, but could you please provide me some information.
-----
When the user selects particular node, and presses the send button, i want to retreive some data in an xml format and send it into an other table.
I have tried some tests with retreiving xml and sending and they are working fine...
but what i would like to know is how to formulate the action for the button, so can u just tell me how to go about it....where to write this code and some code examples, snippets,etc....

if my question is not clear then ill explain in detail again. :-)
0
 
zzynxSoftware engineerCommented:
>> thanks its working
Good. Thanks for accepting.

>> how to formulate the action for the button
yourButton.addActionListener( new ActionListener() {

     public void actionPerformed(ActionEvent e) {
           DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent(); // the selected node
           YourObject obj = (YourObject) nodeSelected.getUserObject();
           String angNumber = null;
           if ( obj.getType() == YourObject.ANGNR ) {      // 1) the selected node is an angnr node
              angNumber = obj.getNumber();
           }
           else if ( obj.getType() == YourObject.VERNR ) {  // 2) the selected node is an version number node
              DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) nodeSelected.getParent(); // get it's parent node (the angnr node)
              obj = (YourObject)parentNode.getUserObject();
              angNumber = obj.getNumber();  // this is the angnr the selected vernr node belongs to
           }
           else if ( obj.getType() == YourObject.UANR ) {
              DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) nodeSelected.getParent().getParent(); // get the parent node of its parent node
              obj = (YourObject)parentNode.getUserObject();
              angNumber = obj.getNumber();  // this is the angnr the selected ua nr node belongs to
           }
           
           // Here you start retreiving your data with angNumber being "the key" to what to query
           // Send to other table
     }
}

Main idea:

    DefaultMutableTreeNode nodeSelected = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent(); // the selected node

give you ***at all times*** the selected node, not only in valueChanged()

Remark:

>>treeSelectionObject@171f189
>>                    |
>>                    treeSelectionObject@171f189
>>                                     |
>>                                  treeSelectionObject@171f189

Sure that number after the '@' sign was always the same?
Because that indicates that those three nodes all have the same treeSelectionObject. I doubt it.

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 14
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now