?
Solved

Comparing treePath selections

Posted on 2004-08-19
20
Medium Priority
?
204 Views
Last Modified: 2009-07-29
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.
0
Comment
Question by:rjopata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 7
  • 3
20 Comments
 
LVL 35

Expert Comment

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

Author Comment

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

Expert Comment

by:girionis
ID: 11842914
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

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

Expert Comment

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

Author Comment

by:rjopata
ID: 11843531
thanks.lets see what others have.someone might come up with a magic touch.
0
 
LVL 92

Expert Comment

by:objects
ID: 11846790
A Set would would perhaps handle it better than a Vector.
0
 

Author Comment

by:rjopata
ID: 11849599
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
 
LVL 92

Expert Comment

by:objects
ID: 11849622
Set is a collection where all elements are unique.
http://java.sun.com/docs/books/tutorial/collections/interfaces/set.html
0
 

Author Comment

by:rjopata
ID: 11850642
thanks objects for the link provided.
0
 

Author Comment

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

Expert Comment

by:girionis
ID: 11858229
0
 

Author Comment

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

Author Comment

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

Expert Comment

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

Author Comment

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

Accepted Solution

by:
girionis earned 300 total points
ID: 11867645
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
 
LVL 92

Expert Comment

by:objects
ID: 11867673
what else would go in the map?
0
 

Author Comment

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

Expert Comment

by:girionis
ID: 11914177
:)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

752 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