0

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:

enter image description here

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.

naeem
  • 43
  • 8
  • don't look recursive to me... – snap Apr 18 '18 at 06:20
  • sorry @snap the next child is get by loop and the with the help of iterator it find the key by comparing value and then delete it – naeem Apr 18 '18 at 06:24
  • how we can do it by recursive ? – naeem Apr 18 '18 at 06:29
  • After indenting your code, it doesn't look like the braces match up. There's either no closing brace for the `for` loop, or the last line is still inside the outer `if` statement. Please correct to match your actual code and please indent your code properly in the future. – David Conrad Apr 18 '18 at 06:32
  • @DavidConrad thanks for your advice, now i have written the complete code and its description to. – naeem Apr 18 '18 at 06:49
  • all the code is i written behind the remove button – naeem Apr 18 '18 at 06:50

0 Answers0