4 Replies Latest reply: Jul 4, 2011 9:48 AM by DarrylBurke RSS

    Custom Tree Cell Editor (Selection Issue)

      Hello there:

      I have added my own TreeCellEditor to a JTree. This editor overloads AbstractCellEditor. Each of the nodes in my tree is a JPanel containing a JCheckBox and a JLabel. I am allowing the user to just click the checkbox (without changing the tree selection) or to just click on the label part and cause the item to be selected (without the checkbox being affected). Everything is working fine except that the tree will no longer allow me to multi-select items. It keeps clearing the selection every time a new item is selected. If I remove my cell editor the multi-selection works again so I know it has to do with my TreeCellEditor but I cannot figure out why. I have shown my code below. If anyone has any advice that would be great.


      private class TreeDelegateCellEditor extends AbstractCellEditor
      implements TreeCellEditor, ActionListener {

      public TreeDelegateCellEditor() {
      jCellRenderer.jCellRendererLabelCheckBox.jCheckBox.addActionListener(this); // LabelCheckBox is my class that contains a panel/label & checkbox

      public Object getCellEditorValue() {
      return jCurrentClickedTreeNode.getUserObject();

      public Component getTreeCellEditorComponent(
      JTree tree,
      Object value,
      boolean isSelected,
      boolean expanded,
      boolean leaf,
      int row) {
      return jCellRenderer.getTreeCellRendererComponent(
      tree, value, isSelected,
      expanded, leaf, row, true);

      public void actionPerformed(ActionEvent e) {

      public boolean shouldSelectCell(EventObject anEvent) {

      // We override this method to avoid the treenode from being
      // selected when the user clicks just on the checkbox
      JCheckBox cb = jCellRenderer.jCellRendererLabelCheckBox.jCheckBox;
      MouseEvent me = (MouseEvent) anEvent;
      if (cb.contains(SwingUtilities.convertPoint(jTree, me.getPoint(), cb))) {
      // So no selection when checkbox is ticked
      return false;
      return true;
        • 1. Re: Custom Tree Cell Editor (Selection Issue)
          If you remove the shouldSelectCell method, does that fix the multi-selection problem?
          • 2. Re: Custom Tree Cell Editor (Selection Issue)
            No that does not fix the issue. I tried that initially and I also tried always returning true from that method - still does not work. By the way the selection mode that I am using is DISCONTIGUOUS_TREE_SELECTION.
            • 3. Re: Custom Tree Cell Editor (Selection Issue)
              I was finally able to figure out what is going on here. I had to override isCellEditable and return true if and only if the user was actually editing the checkbox. In all other cases the cell is not editable and the selection is preserved.

              public boolean isCellEditable(EventObject anEvent) {

              JCheckBox cb = jCellRenderer.jCellRendererLabelCheckBox.jCheckBox;
              MouseEvent me = (MouseEvent) anEvent;
              TreePath path = jTree.getClosestPathForLocation(me.getX(), me.getY());
              if (path != null) {
              Rectangle nodeBounds = getPathBounds(path);
              int cbX = (int) (me.getX() - nodeBounds.getX());
              int cbY = (int) (me.getY() - nodeBounds.getY());
              Point p = new Point(cbX, cbY);
              if (cb.contains(p)) {
              return true;
              return false;
              • 4. Re: Custom Tree Cell Editor (Selection Issue)
                Moderator action: Moved from Java Programming.
                Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

                Then edit your post and format the code correctly.