title: Jtree Select only Child Node and not Parent
body: <p>I am using a Jtree where am stuck at a place to do the below,</p>
  
  <p><img src="https://i.stack.imgur.com/2VuK2.png" alt="Diagram"></p>
  
  <p><strong>From Fig:2</strong>   <code>I click on A(Check box) and all its child is getting selected - It's fine</code></p>
  
  <p><strong>From Fig:3</strong> 
  <code>I Clicked D(CheckBox) and all its parents (A,B,C) are also geting selected.</code></p>
  
  <p>Now what i want to do is,</p>
  
  <pre><code>If i select A -&gt; A,B,C,D will eb selected
  If i Select B-&gt; only B,C,D
  If c -&gt; C,D
  If D-&gt; Only D
  </code></pre>
  
  <p>The Code i use is:</p>
  
  <pre><code>public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){ 
  
                  //delegate is TreeCellrenderer
                  Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 
                  TreePath path = tree.getPathForRow(row);
                  if(path!=null)
                  {         
                      //Selection Model is TreeSelection Model
                      if(selectionModel.isPathSelected(path, true)) //Below code.
                      {       
                          checkBox.setState(checkBox.SELECTED);   
                          if (selected) 
                          {
                              //Comes here If check box is selected by mouse click
                          }
                      }
                      else
                      {                       
                          checkBox.setState(checkBox.NOT_SELECTED);   
                      }
                  } 
                  add(checkBox, BorderLayout.WEST); 
                  add(renderer, BorderLayout.CENTER);  
                  return this; 
              }
  </code></pre>
  
  <p><strong>The isPathSelected Method:</strong></p>
  
  <pre><code>// tells whether given path is selected. 
          // if dig is true, then a path is assumed to be selected, if 
          // one of its ancestor is selected. 
          public boolean isPathSelected(TreePath path, boolean dig){                      
              if(!dig){
                  return super.isPathSelected(path); 
              }
  
              while(path!=null &amp;&amp; !super.isPathSelected(path)){
                  path = path.getParentPath(); 
              }
  
              return path!=null; 
          } 
  </code></pre>
  
  <p>Am not sure whether am doing it roght am new to Jtree. Please help</p>