本文实例为大家分享了java JTree JCheckBox树复选框展示的具体代码,供大家参考,具体内容如下CheckTreeManager.javapublic class CheckTreeManager extends MouseAd
本文实例为大家分享了java JTree JCheckBox树复选框展示的具体代码,供大家参考,具体内容如下
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(); } }
--结束END--
本文标题: java JTree JCheckBox树复选框详解
本文链接: https://lsjlt.com/news/221081.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0