title: Check Box Node Tree , Custom renderer in Swing
body: <p>I have been trying to implement a CheckBox Node Tree, where the parent nodes also have checkbox.</p>
  
  <p>I have been able to achieve the structure . </p>
  
  <p>But when i try to implement the CellEditor , i am not able to check the leaf nodes .The parent nodes are checkable , but not the leaf nodes. Please give me some guidance .  </p>
  
  <pre><code>     /**
       *CheckBoxNodeRenderer.java
       */   
  
      public class CheckBoxNodeRenderer implements TreeCellRenderer{
  public JCheckBox getLeafRenderer()
  {
      return leafRenderer;
  }
  public CheckBoxNodeRenderer() 
  {
      Font fontValue;
      fontValue = UIManager.getFont("Tree.font");
      if (fontValue != null) {
          leafRenderer.setFont(fontValue);
      }
      Boolean booleanValue = (Boolean) UIManager
      .get("Tree.drawsFocusBorderAroundIcon");
      leafRenderer.setFocusPainted((booleanValue != null)
              &amp;&amp; (booleanValue.booleanValue()));
      selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
      selectionForeground = UIManager.getColor("Tree.selectionForeground");
      selectionBackground = UIManager.getColor("Tree.selectionBackground");
      textForeground = UIManager.getColor("Tree.textForeground");
      textBackground = UIManager.getColor("Tree.textBackground");
  }
  public Component getTreeCellRendererComponent(JTree tree, Object value,
          boolean selected, boolean expanded, boolean leaf, int row,
          boolean hasFocus) 
  {
      Component returnValue;
      if (leaf) {
          String stringValue = tree.convertValueToText(value, selected,
                  expanded, leaf, row, false);
          if (selected) {
              leafRenderer.setForeground(selectionForeground);
              leafRenderer.setBackground(selectionBackground);
          } else {
              leafRenderer.setForeground(textForeground);
              leafRenderer.setBackground(textBackground);
          }
          if ((value != null) &amp;&amp; (value instanceof DefaultMutableTreeNode)) {
              Object userObject = ((DefaultMutableTreeNode) value)
              .getUserObject();
              if (userObject instanceof CheckBoxNode) {
                  CheckBoxNode node = (CheckBoxNode) userObject;
                  leafRenderer.setText(node.getText());
                  leafRenderer.setSelected(node.isSelected());
              }
          }
          returnValue = leafRenderer;
      } else {
  
          leafRenderer.setText(value.toString());
  
          leafRenderer.setSelected(selected);
          returnValue = leafRenderer;
  
      }
      return returnValue;
  }
  public JCheckBox leafRenderer = new JCheckBox();
  private DefaultTreeCellRenderer nonLeafRenderer = new DefaultTreeCellRenderer();
  Color selectionBorderColor, selectionForeground, selectionBackground,
  textForeground, textBackground;
  </code></pre>
  
  <p>}</p>
  
  <p>The code below is the checkBoxNodeEditor.java</p>
  
  <pre><code>   /**
     *CheckBoxNodeEditor.java
     */
  
  
  public class CheckBoxNodeEditor extends AbstractCellEditor implements TreeCellEditor {
  public CheckBoxNodeEditor(JTree tree) {
      this.tree = tree;
  }
  /**
   * Checkbox node editor for node selection
   */
  public  Object getCellEditorValue() {
      GenerateCube.nodeselection();
      return GenerateCube.selectNode;
  }
  /**
   * Cell Editable
   */
  public boolean isCellEditable(EventObject event) {
      boolean returnValue = false;
      if (event instanceof MouseEvent) {
          MouseEvent mouseEvent = (MouseEvent) event;
  
          path = tree.getPathForLocation(mouseEvent.getX(),
                  mouseEvent.getY());
          model = (DefaultTreeModel) (tree.getModel());
          if (path != null) {
                       if(path.getChildCount &gt; 0) {
                       Object node = path.getPathComponent(0);
                        }
                        else{
                             Object node = path.getLastPathComponent();
                            }
  
              Object node = path.getLastPathComponent();
              if ((node != null) &amp;&amp; (node instanceof                           DefaultMutableTreeNode)) {
                  DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) node;
                  Object userObject = treeNode.getUserObject();
                  returnValue = ((treeNode.isLeaf()) &amp;&amp; (userObject instanceof CheckBoxNode));
              }
          }
      }
      return returnValue;
  }
  /**
   * getting Tree Cell Editor Component
   */
  public Component getTreeCellEditorComponent(JTree tree, Object value,
          boolean selected, boolean expanded, boolean leaf, int row) {
      Component editor = GenerateCube.renderer.getTreeCellRendererComponent(tree, value,
              true, expanded, leaf, row, true);
      // editor always selected / focused
      ItemListener itemListener = new ItemListener() {
          public void itemStateChanged(ItemEvent itemEvent) {
              if (stopCellEditing()) {
                  fireEditingStopped();
              }
          }       };
          if (editor instanceof JCheckBox) {
              ((JCheckBox) editor).addItemListener(itemListener);
          }
          return editor;
  }
  
  
  static int n;
  public static  JTree tree;
  static DefaultTreeModel model ;
  ChangeEvent changeEvent = null;
  public static  TreePath path ;
  public static String parentstring;
  
  public static DefaultMutableTreeNode nNode;
  public static MutableTreeNode node;
  String nodeName;
  </code></pre>
  
  <p>}</p>