Solved

Comparing treePath selections

Posted on 2004-08-19
20
199 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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