Link to home
Start Free TrialLog in
Avatar of rjopata
rjopata

asked on

Storing nodes under TreeSelectionListener

fiinal JTree tree=new JTree();
tree.addTreeSelectionListener(new TreeSelectionListener()
                   {
               public void valueChanged( TreeSelectionEvent event )
                    {
                   if( event.getSource() == tree )
                         {
                               TreePath paths[] = tree.getSelectionPaths();
                        if(paths==null)
                         return;
                        for(int i=0;i<paths.length;i++){
                          if(event.getNewLeadSelectionPath()!=null){
                    System.out.println(paths[i]);
                                   }
                    }
               }
               }
          });

Observation: paths[i] prints both a single selected node/child node and multiple selection as well.
Question : how do i store the single selected nodes in an array for example.that is with a click on a node,that node selected is stored;the next selected node is added to  the already stored one and so on and on and on.
is it possible to carryout this on multiple selection as well ?

Thanks for you patience and time.
Avatar of girionis
girionis
Flag of Greece image

Have a collection object (or an array if you know before hand how many nodes will be selected) and then add each node selection there. Your collection should be a member variable.
Avatar of rjopata
rjopata

ASKER

thanks.i go for a vector.how will the code look like considering my initial posting please.
Vector nodesSelected = new Vector();

then inside your value changed method do somethign like:

for(int i=0;i<paths.length;i++){
                          if(event.getNewLeadSelectionPath()!=null){
                    nodesSelected.addElement(paths[i]);  
                    System.out.println(paths[i]);
                                   }
Avatar of rjopata

ASKER

Thanks grionis but when i print the contents of the vector the initial selected nodes are not stored but only the currently selected one. ie
 Vector nodesSelected = new Vector();
       for(int i=0;i<paths.length;i++){
                          if(event.getNewLeadSelectionPath()!=null){
                    nodesSelected.addElement(paths[i]);  
                    System.out.println(paths[i]);
                                   }
  System.out.println(nodesSelected);

i want to see all the nodes selected in the vector please.
Avatar of rjopata

ASKER

Specifically only nodes that are multiple selected are stored not with single selection.
First of all, is the vector an instance variable or local to the method? If local to the method then make it an instance variable.

Second, where do you print the single selected nodes? You need to add the nodes in the vector as well.
Avatar of rjopata

ASKER

> is the vector an instance variable or local to the method?
the vector is an instance variable.

>Second, where do you print the single selected nodes?
it should not be printed seperately from the multiple selected nodes.if i decide to select single nodes ,the vector should print all single nodes selected and if i go for multiple selection the same vector should print all the multiple selected nodes.

am i reaching u.
thanks
Ok, from this one:

> System.out.println(nodesSelected);

> i want to see all the nodes selected in the vector please.

you print out the vector itself, with all the data in there. If it does not have the single selection nodes it means that you do not add them. Where do you add them to the vector (if at all)?
Avatar of rjopata

ASKER

how do i add both single and multiple selections
In the method you are reading the single selections.
Avatar of rjopata

ASKER

the following code with the print statement will also print single selection if i dont hold down the ctr key. is that rigtht ?
                for(int i=0;i<paths.length;i++){
                          if(event.getNewLeadSelectionPath()!=null){
                    nodesSelected.addElement(paths[i]);  
                    System.out.println(paths[i]);
                                   }
It should yes, so the data should be in the vector. Are you sure it's not there? What happens if you do:

for (int i=0; i<nodesSelected.size(); i++)
{
    nodesSeleected.elementAt(i);
}
Avatar of rjopata

ASKER

sorry girionis, multiple selections works fine but not with single selections.the data seems not to be there.only the currently selected one is printed out.the old selections dont show up in the vector
Weird.. If you store them they should be here.Can you post a small compilable example that demonstrates the problem?
Avatar of rjopata

ASKER

to be a little bit clearer, if u try printing the index i, u realised it is constantly 0 and only increases when multiple selection is in use.i only increases from 0 by holding down ctr key or using multiple selection.
does this mean something?
It shouldn't matter since you increment the vector naturally and not based on any index. even if you have just one selection it should enter the for loop since the paths.length is 1. Does it eneter the for loop if there is only one selection? And does the paths array changes or does it alaways hold the same reference?
Avatar of rjopata

ASKER

well well girionis it does and the elements can be retreived perfectly but one more question.
if i want to use this vector again and again then ofcourse i have to creat a variable of the vector local to the method to clear its contents.in this case single selection is not working when i print the contents of the vector. have any reason why.
thanks for ya help.
> if i want to use this vector again and again then ofcourse i have to
> creat a variable of the vector local to the method to clear its contents.

Why do you need to use the same vector over and over? You might as well create local vectors and use them.
Avatar of rjopata

ASKER

just trying to experiment.assuming we have only one vector then ?
I am not sure why. I will need to see some code in order to see what might be going wrong.
Avatar of rjopata

ASKER

well u have this code then
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.util.*;

 class ExampleTree extends JFrame
 {

      private      JPanel            topPanel;
      private      JTree            tree;
      private      JScrollPane scrollPane;
     private Vector nodesSelected=new Vector();


      public ExampleTree()
      {
            
            setTitle( "Tree Application" );
            setSize( 250, 250 );
            
            
            topPanel = new JPanel();
            topPanel.setLayout( new BorderLayout() );
            getContentPane().add( topPanel );

      
            tree = new JTree();

            tree.addTreeSelectionListener(new TreeSelectionListener(){
                  public void valueChanged( TreeSelectionEvent event )
                  {
            if( event.getSource() == tree )
            {
                 nodesSelected=new Vector();
            
                  TreePath[] paths = tree.getSelectionPaths();
                  if(paths==null)
                      return;
                  for(int i=0;i<paths.length;i++){
                      nodesSelected.addElement(paths[i]);
                      
                  }
                  System.out.println(nodesSelected);
            }
                  }
                });

            
            scrollPane = new JScrollPane();
            scrollPane.getViewport().add( tree );
            topPanel.add( scrollPane, BorderLayout.CENTER );
      }

      
      public static void main( String args[] )
      {
            ExampleTree mainFrame= new ExampleTree();
            mainFrame.setVisible( true );
      }
 }


I do not see anything weird with your code. Can you make sure it enters the for loop?
Avatar of rjopata

ASKER

maybe u can look at it again.
Vector nodesSelected is declared twice .first as an instance and then local with public void valueChanged(){
in this  case single selection do not work and only multiple selection.
can u kindly cross check please.
It is not declared twice, this would be an error if in the same scope, it is initialized twice. But this shouldn't be a problem. It is initialized inside the method and it is used inside the method too. I see no problem with that. Maybe there is some other code that modifies the vector?
Avatar of rjopata

ASKER

sorry for wrong choice of words.it is initialized twice.
the code is just as i have posted and there is nothing more.i can make what is wrong out.
I just ran the code here and it works fine... What exactly do you mean it does not display single selections?
Avatar of rjopata

ASKER

what i mean is that the vector nodesSelected does not print previously selected nodes but only currently selected nodes when nodes are selected in singles.this is not the case for multiple selections
Ok I see what you mean now. This is because the multiple nodes are already selected, so the paths array holds the previous selected nodes as well.

On single selection none of them is selected and because you re-initialise the vector the previous values get lost. This is the case with multiple selections, the previous values do get lost, but because the nodes are still selected on the jtree they are put in the new vector again.
Avatar of rjopata

ASKER

does make perfect sense.is there a workaround it or one should not re-initialize the vector?
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rjopata

ASKER

now that things are clear i would like to extend my question please.
Assuming i have on a seperate panel  4 comb boxes whose items are to be selected and assigned to either a single selected node or a multiple selected node.ie for each selected node(single or multiple) 4 items selected from the 4 combo boxes are assigned to it(them).How do i retrieve the items under the current selected node or nodes from the combo boxes.

Hmm.. not sure how you could go on about it. You will need to have a listener on the combo boxes and update the nodes when you select them.
Avatar of rjopata

ASKER

Thanks.looks pretty tricky.
The question for me is where exactly do u listen to the combo item selections ?is it under tree selections or within the actionPerformed for the combo boxes.How do u synchronize these two listeners.very confusing i guess.
YOu listen to the combonent from where you want to get the items from, in your case the combo box. It has its own listeners, better take a look at the JComboBox documents to see what exactly is going on. It is not complicated, you just need to use the proper listeners :)