本文实例为大家分享了java JTree JCheckBox树复选框展示的具体代码,供大家参考,具体内容如下
成都创新互联专注于网站建设,为客户提供网站制作、成都网站设计、网页设计开发服务,多年建网站服务经验,各类网站都可以开发,品牌网站制作,公司官网,公司展示网站,网站设计,建网站费用,建网站多少钱,价格优惠,收费合理。1.CheckTreeManager.java
public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener { private CheckTreeSelectionModel selectionModel = null; // private JTree tree = new JTree(); private JTree tree = null; int hotspot = new JCheckBox().getPreferredSize().width; public CheckTreeManager(JTree tree) { this.tree = tree; selectionModel = new CheckTreeSelectionModel(tree.getModel()); tree.setCellRenderer(new CheckTreeCellRenderer(tree.getCellRenderer(), selectionModel)); tree.addMouseListener(this); //鼠标监听 selectionModel.addTreeSelectionListener(this); //树选择监听 } public void mouseClicked(MouseEvent me) { TreePath path = tree.getPathForLocation(me.getX(), me.getY()); if(path==null) return; if(me.getX()>tree.getPathBounds(path).x+hotspot) return; boolean selected = selectionModel.isPathSelected(path, true); selectionModel.removeTreeSelectionListener(this); try { if(selected) selectionModel.removeSelectionPath(path); else selectionModel.addSelectionPath(path); } finally { selectionModel.addTreeSelectionListener(this); tree.treeDidChange(); } } public CheckTreeSelectionModel getSelectionModel() { return selectionModel; } public void valueChanged(TreeSelectionEvent e) { tree.treeDidChange(); } }