この記事では、参照用のJava Jtree JCheckboxツリーチェックボックスに表示される特定のコードを共有します。特定のコンテンツは次のとおりです
1.CHECKTREEMANAGER.JAVA
Public Class CheckTreeMeanager拡張Mouseadapterを実装するgresselectionlistener {private checktreeselectionmodel selectionmodel = null; // private jtree tree = new Jtree();プライベートJtreeツリー= 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(mousevent 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.ispathelected(path、true); selectionModel.RemovetreeSelectionListener(this); try {if(selected)selectionModel.RemoveselectionPath(PATH); else selectionmodel.addselectionpath(path); }最後に{SelectionModel.AddTreeSelectionListener(this); tree.treedidchange(); }} public CheckTreeSelectionModel getSelectionModel(){return SelectionModel; } public void ValueChanged(treeselectionEvent e){tree.treedidchange(); }} 2.CheckTreeSelectionModel.java
Public Class CheckTreeSelectionModelは、defaultTreeSelectionModel {private treemodelモデルを拡張します。 public CheckTreeSelectionModel(TreeModel Model){this.model = model; setSelectionMode(treeselectionModel.discontiguous_tree_selection); } //指定されたパスのサブツリーに選択されていないノードがあるかどうかをテストしますパブリックブールンISPartiviallySected(Treepath Path){if(ispathselected(path、true))return false; Treepath [] SelectionPaths = getSelectionPaths(); if(selectionpaths == null)falseを返します。 for(int j = 0; j <selectionpaths.length; j ++){if(isdescendant(selectionpaths [j]、path))true; } falseを返します。 } //与えられたパスが選択されているかどうかを示します。 // DIGが真である場合、//その祖先のいずれかが選択されている場合、パスが選択されると想定されます。 public boolean ispathselected(treepath path、boolean dig){if(!dig)return super.ispathselected(path); while(path!= null &&!super.ispathselected(path))path = path.getParentPath(); return path!= null; } // path1 path2 path2 private boolean isdescendant(treepath path1、treepath path2){object obj1 [] = path1.getpath();オブジェクトobj2 [] = path2.getPath(); for(int i = 0; i <obj2.length; i ++){if(obj1 [i]!= obj2 [i])falseを返す; } trueを返します。 } public void setSelectionPaths(treepath [] ppaths){新しいunsupportedoperationexception( "まだ実装されていない!!!"); } public void addSelectionPaths(treepath [] paths){//すべての子孫のパスのすべての子孫[] for(int i = 0; i <paths.length; i ++){treepath path = paths [i]; Treepath [] SelectionPaths = getSelectionPaths(); if(selectionpaths == null)break; arrayList tobereMoved = new ArrayList(); for(int j = 0; j <selectionpaths.length; j ++){if(isdescendant(selectionpaths [j]、path))toberemoved.add(selectionpaths [j]); } super.removeselectionpaths((treepath [])toberemoved.toarray(new Treepath [0])); } //すべての兄弟が選択されている場合は、それらを選択せず、親を再帰的に選択します//そのパスを選択するだけです。 for(int i = 0; i <paths.length; i ++){treepath path = paths [i]; Treepath temp = null; while(aresiblingselected(path)){temp = path; if(path.getParentPath()== null)break; path = path.getParentPath(); } if(temp!= null){if(temp.getParentPath()!= null)addSelectionPath(temp.getParentPath()); else {if(!selectionempty())removeSelectionPaths(getSelectionPaths()); super.addselectionPaths(new Treepath [] {temp}); }} else super.addselectionPaths(new Treepath [] {path}); }} //与えられたパスのすべての兄弟が選択されているかどうかを示します。 Private Boolean aresiblingselected(Treepath Path){treepath parent = path.getParentPath(); if(parent == null)trueを返します。 Object node = path.getLastPathComponent(); Object ParentNode = parent.getLastPathComponent(); int upridcount = model.getChildCount(parentNode); for(int i = 0; i <childCount; i ++){object childnode = model.getChild(parentnode、i); if(childnode == node)継続; if(!ispathselected(parent.pathbyaddingchild(childnode)))return false; } trueを返します。 } public void removeSelectionPaths(treepath [] paths){for(int i = 0; i <paths.length; i ++){treepath path = paths [i]; if(path.getPathCount()== 1)super.removeselectionPaths(new Treepath [] {path}); else toggleremoveselection(path); }} //指定されたパスの祖先ノードが選択されている場合は、それを選択していない//与えられたパスと子孫を除くすべての子孫を選択します。 //それ以外の場合は、指定されたパスを解除するだけで、private void toggremoveselection(treepath path){stack stack = new stack(); treepath parent = path.getParentPath(); while(parent!= null &&!ispathselected(parent)){stack.push(parent);親= parent.getParentPath(); } if(parent!= null)stack.push(parent); else {super.removeselectionPaths(new Treepath [] {path});戻る; } while(!stack.isempty()){treepath temp =(treepath)stack.pop(); treepath peekpath = stack.isempty()?パス:( treepath)stack.peek(); Object node = temp.getLastPathComponent(); Object Peeknode = PeekPath.getLastPathComponent(); int charpled = model.getChildCount(node); for(int i = 0; i <childcount; i ++){object childnode = model.getChild(node、i); if(childnode!= peeknode)super.addselectionPaths(new Treepath [] {temp.PathByAddingChild(ChildNode)}); }} super.removeselectionPaths(new Treepath [] {parent}); }} 3.CHECKTREECELLRENDER .JAVA
Public Class CheckTreeCellRenderer拡張jPanel実装treecellRenderer {private checkTreeSelectionModel SelectionModel;プライベートTreeCellRenderer Delegate; // private tristatecheckboxチェックボックス= new tristatecheckbox();プライベートJCheckboxチェックボックス= new JCheckbox(); public CheckTreeCellRenderer(TreeCellRenderer Delegate、CheckTreeSelectionModel SelectionModel){this.delegate = Delegate; this.selectionModel = selectionModel; setLayout(new borderlayout());セトパック(false); checkbox.setopaque(false); } publicコンポーネントgetTreeCellRendererComponent(JTree Tree、オブジェクト値、ブール値、ブール拡張、ブール葉、int row、boolean hasfocus){コンポーネントrenderer = delegate.gettrecellerendercomponent(ツリー、値、選択、拡張、拡張、葉、row、hasfocus); Treepath Path = tree.getPathForrow(row); if(path!= null){system.out.println(path); if(selectionModel.Ispathelected(path、true))checkbox.setselected(true); else {system.out.println(selectionmodel.ispartivitielySelected(PATH)); checkbox.setselected(selectionmodel.ispartivitielyselected(path)?true:fals); }} removeall();追加(チェックボックス、borderlayout.west); add(renderer、borderlayout.center);これを返します。 }} 4。使用法
checktreemanager checktreemanager = new checktreemanager(jtree);
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。