Comparing treePath selections

I want to keep track of nodes selected in  a tree such that a node or  a child which is already selected cannot be selected again.i tried using getLeadSelectionPath() and getOldLeadSelectionPath() but the comparison was not producing any good results.i also thought of  using  Object[] path=paths[i].getPath() to store the nodes in say String[] and probably loop through to see whether a new selected item is already stored.
Can anybody help...
Thanks in advance.
rjopataAsked:
Who is Participating?
 
girionisConnect With a Mentor Commented:
String [] array = {"1", "2", "3", "4"};
         
HashMap map = new HashMap();
map.put("numbers", array);
String [] ar = (String []) map.get("numbers");

The "numbers" is the key of the hash map.

To iterate through the elements of the "ar" you can do:

for (int i=0; i<ar.length; i++)
{
    System.out.println(ar[i]);
}
0
 
girionisCommented:
Hi rjopata,

can you not just disable the nodes you visit? Call getTreeCellEditorComponent()  and pass it the node. Then call setEnable(false) on the component. This should disable it and not be able to select it again.

Regards
0
 
rjopataAuthor Commented:
Hi girionis thanks for ur quick response.i actually dont want to disable it completely becuase that prevents any further action to be taken on that node.rather i want to let the user know the node has been selected and if there is a change in decision something else could be done to the node then.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
girionisCommented:
Since you already have the node selected can you not put it in a Vector? Then with each other node selected just go throuhg the vector and see if it is already there. Can you post some code?
0
 
rjopataAuthor Commented:
that was exactly my initial intention as i posted.but the problem i forsee is with a large tree then lots of data needs to be stored just to locate a child down the tree and thought probably there exists a better way out.
Sorry i cannot post some code now since i wanted to have opinions before trying to put in an array or Vector as u put it.
0
 
girionisCommented:
I am not sure of any better way but I would stick with a collections object, at least for start. Make it work first and optimize later :) But it would be intersting what othre peopel can come up with, lets wait for more suggestions.
0
 
rjopataAuthor Commented:
thanks.lets see what others have.someone might come up with a magic touch.
0
 
objectsCommented:
A Set would would perhaps handle it better than a Vector.
0
 
rjopataAuthor Commented:
sounds like the expert is approving the intended solution but with a better option.
i never used Set, would u like to educate 'objects' please?
0
 
objectsCommented:
Set is a collection where all elements are unique.
http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html
0
 
rjopataAuthor Commented:
thanks objects for the link provided.
0
 
rjopataAuthor Commented:
objects ur link presents a solution but i'm still not clear abt this problem i have here :
a node/child selected in the tree can be assigned 4 or even more values say from a combo box.
how do i keep track of these two distinct values ie the node/child and the values assigned from the combo box.in my case i need to store both the node and its distinct corresponding values from the combo box so that if i reselect the node i know oh this node is already selected and has that distinct values.
please any ideas ????
0
 
girionisCommented:
0
 
rjopataAuthor Commented:
Sorry for missing out on ur comment girionis.well i guess with the HashMap using Object put(Object key, Object value) will do but in this case Object value itself is a String [] consisting of say 4 of more items.iterating through the HashMap will surely not retrieve the String [] items and might event  result in a class cast exception.
0
 
rjopataAuthor Commented:
can somebody rewrite the code below or produce a similar one to reflect what i'm thinking of
basically i want to store elements from the combo box in an ArrayList and with a selected node use the hashmap appropriately.

HashMap collect=new HashMap();
//initialize ArrayList with selected items from combo box
ArrayList ar=new ArrayList();
ar.add(combo1.getSelectedItem());
ar.add(combo2.getSelectedItem());
ar.add(combo3.getSelectedItem());
ar.add(combo4.getSelectedItem());
ar.add(combo5.getSelectedItem());
//put items in hashmap
m.put(new String("node selected"),ar);
//print out key and values
System.out.println(m)
0
 
girionisCommented:
>  will do but in this case Object value itself is a String [] consisting of say 4 of more items

I do not see this being a problem

String [] array = {"1", "2", "3", "4"};
            
HashMap map = new HashMap();
map.put("array", array);
String [] ar = (String []) map.get("array");
0
 
rjopataAuthor Commented:
thanks girionis for the above.well i guess i have to carefully read the Map interface to get things right but can u kindly produce a code that will retrieve the key in the above case as well as the elements of the String (not as a set).
0
 
objectsCommented:
what else would go in the map?
0
 
rjopataAuthor Commented:
> what else would go in the Map ?
objects distinct nodes and corresponding selected items from a combo box.so for a selected node i collect items selected in a combo box and put all in a hashmap.the idea is to show the items in the combo box back again if the same node is reselected.
0
 
girionisCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.