Solved

Comparing treePath selections

Posted on 2004-08-19
20
200 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 100 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Systems talking to each other 5 124
servlet doXXX methods 3 35
fibonacci ten numbers 4 26
Way to decrease size of apk file 9 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now