I have a JTree. I tried to remove nodes recursively. I have written the following code for it. First in the if statement i have written the code if the node has no leaf then it delete it and from the linkedHashMap also. Second in the elsa statement i have written the code that first it will find that a particular node have how many children through loop then in the iterator method it compare the child name and find the key and del it from linkedHashMap too.
selectednode = (DefaultMutableTreeNode) TreePro.getLastSelectedPathComponent();
DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectednode.getParent();
if(selectednode != null){
if (selectednode.isLeaf()) {
Iterator<Map.Entry<Integer, String>> irt = col.entrySet().iterator();
while(irt.hasNext())
{
Map.Entry<Integer, String> entry = irt.next();
if(selectednode.isLeaf() && entry.getValue().equals(TextField2.getText()))
{
System.out.println(" Removed. "+entry.getKey());
irt.remove(); // Call Iterator's remove method.
node.remove(selectednode);
System.out.println("LinkedHashMap Size : "+col.size());
model.reload(node);
}
}
}
// The problem begin from here
else{
int p = JOptionPane.showConfirmDialog(null, "Warning "+selectednode+ " is a Parent node, It will DEL all his child nodes" , "Delete",JOptionPane.YES_NO_OPTION);
if(p == 0){
for (int i = 0; i < selectednode.getChildCount(); i++) {
TreeNode nodee = selectednode.getChildAt(i);
String batie = nodee.toString();
System.out.println("batreeee "+batie);
System.out.println("break time "+selectednode.getChildAt(i));
Iterator<Map.Entry<Integer, String>> itt = col.entrySet().iterator();
while(itt.hasNext())
{
Map.Entry<Integer, String> entryy = itt.next();
if( entryy.getValue().equals(TextField2.getText()))
{
System.out.println(" Removed. "+entryy.getKey());
itt.remove(); // Call Iterator's remove method.
node.remove(selectednode);
System.out.println("LinkedHashMap Size : "+col.size());
model.reload(node);
}
if( entryy.getValue().equals(batie))
{
System.out.println(" Removed. "+entryy.getKey());
itt.remove(); // Call Iterator's remove method.
System.out.println("LinkedHashMap Size : "+col.size());
model.reload(node);
}
}
}selectednode.removeAllChildren();
}}}}
now the case is:
In the above pic when i remove the color node it del all his child from the linkedlist except Dark Blue and Light Blue. i tried many things but unable to understand why is that.