[Webinar] Streamline your web hosting managementRegister Today

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

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.
0
rjopata
Asked:
rjopata
  • 17
  • 17
1 Solution
 
girionisCommented:
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.
0
 
rjopataAuthor Commented:
thanks.i go for a vector.how will the code look like considering my initial posting please.
0
 
girionisCommented:
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]);
                                   }
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
rjopataAuthor Commented:
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.
0
 
rjopataAuthor Commented:
Specifically only nodes that are multiple selected are stored not with single selection.
0
 
girionisCommented:
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.
0
 
rjopataAuthor Commented:
> 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
0
 
girionisCommented:
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)?
0
 
rjopataAuthor Commented:
how do i add both single and multiple selections
0
 
girionisCommented:
In the method you are reading the single selections.
0
 
rjopataAuthor Commented:
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]);
                                   }
0
 
girionisCommented:
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);
}
0
 
rjopataAuthor Commented:
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
0
 
girionisCommented:
Weird.. If you store them they should be here.Can you post a small compilable example that demonstrates the problem?
0
 
rjopataAuthor Commented:
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?
0
 
girionisCommented:
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?
0
 
rjopataAuthor Commented:
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.
0
 
girionisCommented:
> 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.
0
 
rjopataAuthor Commented:
just trying to experiment.assuming we have only one vector then ?
0
 
girionisCommented:
I am not sure why. I will need to see some code in order to see what might be going wrong.
0
 
rjopataAuthor Commented:
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 );
      }
 }


0
 
girionisCommented:
I do not see anything weird with your code. Can you make sure it enters the for loop?
0
 
rjopataAuthor Commented:
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.
0
 
girionisCommented:
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?
0
 
rjopataAuthor Commented:
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.
0
 
girionisCommented:
I just ran the code here and it works fine... What exactly do you mean it does not display single selections?
0
 
rjopataAuthor Commented:
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
0
 
girionisCommented:
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.
0
 
rjopataAuthor Commented:
does make perfect sense.is there a workaround it or one should not re-initialize the vector?
0
 
girionisCommented:
You should not re-initialize the vector, otherwise it won't be able to hold the single selection and the previosu values as well.
0
 
rjopataAuthor Commented:
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.

0
 
girionisCommented:
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.
0
 
rjopataAuthor Commented:
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.
0
 
girionisCommented:
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 :)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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