java JTree JCheckBox树复选框详解
本文实例为大家分享了javaJTreeJCheckBox树复选框展示的具体代码,供大家参考,具体内容如下
1.CheckTreeManager.java
publicclassCheckTreeManagerextendsMouseAdapterimplementsTreeSelectionListener { privateCheckTreeSelectionModelselectionModel=null; //privateJTreetree=newJTree(); privateJTreetree=null; inthotspot=newJCheckBox().getPreferredSize().width; publicCheckTreeManager(JTreetree) { this.tree=tree; selectionModel=newCheckTreeSelectionModel(tree.getModel()); tree.setCellRenderer(newCheckTreeCellRenderer(tree.getCellRenderer(),selectionModel)); tree.addMouseListener(this);//鼠标监听 selectionModel.addTreeSelectionListener(this);//树选择监听 } publicvoidmouseClicked(MouseEventme) { TreePathpath=tree.getPathForLocation(me.getX(),me.getY()); if(path==null) return; if(me.getX()>tree.getPathBounds(path).x+hotspot) return; booleanselected=selectionModel.isPathSelected(path,true); selectionModel.removeTreeSelectionListener(this); try { if(selected) selectionModel.removeSelectionPath(path); else selectionModel.addSelectionPath(path); } finally { selectionModel.addTreeSelectionListener(this); tree.treeDidChange(); } } publicCheckTreeSelectionModelgetSelectionModel() { returnselectionModel; } publicvoidvalueChanged(TreeSelectionEvente) { tree.treeDidChange(); } }
2.CheckTreeSelectionModel.java
publicclassCheckTreeSelectionModelextendsDefaultTreeSelectionModel { privateTreeModelmodel; publicCheckTreeSelectionModel(TreeModelmodel) { this.model=model; setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); } //testswhetherthereisanyunselectednodeinthesubtreeofgivenpath publicbooleanisPartiallySelected(TreePathpath){ if(isPathSelected(path,true)) returnfalse; TreePath[]selectionPaths=getSelectionPaths(); if(selectionPaths==null) returnfalse; for(intj=0;j3.CheckTreeCellRenderer.java
publicclassCheckTreeCellRendererextendsJPanelimplementsTreeCellRenderer { privateCheckTreeSelectionModelselectionModel; privateTreeCellRendererdelegate; //privateTristateCheckBoxcheckBox=newTristateCheckBox(); privateJCheckBoxcheckBox=newJCheckBox(); publicCheckTreeCellRenderer(TreeCellRendererdelegate,CheckTreeSelectionModelselectionModel){ this.delegate=delegate; this.selectionModel=selectionModel; setLayout(newBorderLayout()); setOpaque(false); checkBox.setOpaque(false); } publicComponentgetTreeCellRendererComponent(JTreetree,Objectvalue,booleanselected,booleanexpanded,booleanleaf,introw,booleanhasFocus){ Componentrenderer=delegate.getTreeCellRendererComponent(tree,value,selected,expanded,leaf,row,hasFocus); TreePathpath=tree.getPathForRow(row); if(path!=null) { System.out.println(path); if(selectionModel.isPathSelected(path,true)) checkBox.setSelected(true); else { System.out.println(selectionModel.isPartiallySelected(path)); checkBox.setSelected(selectionModel.isPartiallySelected(path)?true:false); } } removeAll(); add(checkBox,BorderLayout.WEST); add(renderer,BorderLayout.CENTER); returnthis; } }4.用法
CheckTreeManagercheckTreeManager=newCheckTreeManager(jTree);以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。