Solved

Storing nodes under TreeSelectionListener

Posted on 2004-09-12
34
640 Views
Last Modified: 2008-02-01
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
Comment
Question by:rjopata
  • 17
  • 17
34 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 12041578
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
 

Author Comment

by:rjopata
ID: 12041612
thanks.i go for a vector.how will the code look like considering my initial posting please.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12041638
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
 

Author Comment

by:rjopata
ID: 12041943
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
 

Author Comment

by:rjopata
ID: 12041956
Specifically only nodes that are multiple selected are stored not with single selection.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12042260
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
 

Author Comment

by:rjopata
ID: 12042328
> 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
 
LVL 35

Expert Comment

by:girionis
ID: 12042349
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
 

Author Comment

by:rjopata
ID: 12042413
how do i add both single and multiple selections
0
 
LVL 35

Expert Comment

by:girionis
ID: 12042468
In the method you are reading the single selections.
0
 

Author Comment

by:rjopata
ID: 12042502
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
 
LVL 35

Expert Comment

by:girionis
ID: 12042515
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
 

Author Comment

by:rjopata
ID: 12042841
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
 
LVL 35

Expert Comment

by:girionis
ID: 12042927
Weird.. If you store them they should be here.Can you post a small compilable example that demonstrates the problem?
0
 

Author Comment

by:rjopata
ID: 12042948
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
 
LVL 35

Expert Comment

by:girionis
ID: 12042990
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
 

Author Comment

by:rjopata
ID: 12043204
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 35

Expert Comment

by:girionis
ID: 12043232
> 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
 

Author Comment

by:rjopata
ID: 12043261
just trying to experiment.assuming we have only one vector then ?
0
 
LVL 35

Expert Comment

by:girionis
ID: 12043322
I am not sure why. I will need to see some code in order to see what might be going wrong.
0
 

Author Comment

by:rjopata
ID: 12043542
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
 
LVL 35

Expert Comment

by:girionis
ID: 12043747
I do not see anything weird with your code. Can you make sure it enters the for loop?
0
 

Author Comment

by:rjopata
ID: 12043968
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
 
LVL 35

Expert Comment

by:girionis
ID: 12044036
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
 

Author Comment

by:rjopata
ID: 12044107
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
 
LVL 35

Expert Comment

by:girionis
ID: 12044143
I just ran the code here and it works fine... What exactly do you mean it does not display single selections?
0
 

Author Comment

by:rjopata
ID: 12044230
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
 
LVL 35

Expert Comment

by:girionis
ID: 12044290
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
 

Author Comment

by:rjopata
ID: 12044360
does make perfect sense.is there a workaround it or one should not re-initialize the vector?
0
 
LVL 35

Accepted Solution

by:
girionis earned 100 total points
ID: 12044387
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
 

Author Comment

by:rjopata
ID: 12047961
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
 
LVL 35

Expert Comment

by:girionis
ID: 12051732
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
 

Author Comment

by:rjopata
ID: 12051797
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
 
LVL 35

Expert Comment

by:girionis
ID: 12051856
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

11 Experts available now in Live!

Get 1:1 Help Now