/** * Constructor. * * @param tree a JTree */ public CheckTreeManager(JTree tree) { this.tree = tree; selectionModel = new CheckTreeSelectionModel(tree.getModel()); tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel)); tree.addMouseListener(this); tree.addMouseMotionListener(this); selectionModel.addTreeSelectionListener(this); }
/** * Handles mouse click events. * * @param e the mouse event */ public void mouseClicked(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) return; if (e.getX() > tree.getPathBounds(path).x + hotspot) return; boolean selected = selectionModel.isAncestorSelected(path); try { ignoreEvents = true; if (selected) { selectionModel.removeSelectionPath(path); } else { selectionModel.addSelectionPath(path); } } finally { ignoreEvents = false; tree.treeDidChange(); } }