이 기사에서는 참조를 위해 Java Jtree jcheckbox 트리 체크 확인란에 표시된 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
1. Checktreanager.java
공개 클래스 checktreemanager 확장 MouseAdapter 구현 treeselectionListener {private CheckTreesElectionModel selectionModel = null; // private jtree tree = new jtree (); 개인 jtree tree = null; int hotspot = new Jcheckbox (). getPreferredSize (). 너비; public checktreemanager (jtree tree) {this.tree = tree; selectionModel = new CheckTreeSelectionModel (tree.getModel ()); tree.setCellrenderer (New CheckTreeCellrenderer (tree.getCellrenderer (), SelectionModel); tree.addmouselistener (this); // 마우스 청취 선택 model.addtreeSelectionListener (this); // 트리 선택 청취} public void mouseclicked (mouseevent me) {treepath path = tree.getPathForLocation (me.getx (), me.gety ()); if (path == null) 반환; if (me.getx ()> tree.getPathBounds (Path) .x+핫스팟) 리턴; 부울 선택 = selectionModel.isPathSelected (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
공개 클래스 checkTreesElectionModel은 defaultTreesElectionModel {private treemodel model; public checkTreeSelectionModel (treemodel model) {this.model = model; setSelectionMode (treeselectionModel.DisconTiguous_tree_Selection); } // 주어진 경로의 하위 트리에 선택되지 않은 노드가 있는지 테스트 공개 부울 isparticallySelect (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 isPathErtected (Treepath Path, Boolean Dig) {if (! dig) return super.ispathSelected (path); while (path! = null &&! super.ispathSelected (path)) path = path.getParentPath (); 반환 경로! = null; } // Path2 PRIBAL BOOLEAN ISDESCENDANT (Treepath Path1, Treepath Path2)의 Path1 자손입니다. Object 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) {// (int i = 0; i <paths.length; i ++) {treepath path = paths [i]; treepath [] selectionPaths = getSelectionPaths (); if (selectionPaths == null) 브레이크; ArrayList toberemoved = new ArrayList (); for (int j = 0; j <selectionPaths.length; } super.removeselectionPhents ((Treepath []) toberemoved.toArray (New Treepath [0]); } // 모든 형제 자매가 선택되면 선택을 해제하고 부모를 재귀 적으로 선택하십시오. // Otherwize는 해당 경로를 선택하십시오. for (int i = 0; i <paths.length; i ++) {treepath path = paths [i]; Treepath temp = null; while (aresiblingsselected (path)) {temp = path; if (path.getParentPath () == null) break; PATH = PATH.GETPARENTPATH (); } if (temp! = null) {if (temp.getParentPath ()! = null) addSelectionPath (temp.getParentPath ()); else {if (! isselectionEmpty ()) 제거 선택 경로 (getSelectionPaths ()); super.addselectionPhents (New Treepath [] {temp}); }} else super.addsElectionPaths (new Treepath [] {path}); }} // 주어진 경로의 모든 형제 자매가 선택되었는지 여부를 알려줍니다. 개인 부울 aresiblingsselected (Treepath Path) {treepath parent = path.getParentPath (); if (parent == null) true를 반환합니다. Object Node = path.getLastPathComponent (); Object ParentNode = parent.getLastPathComponent (); int childcount = model.getChildCount (ParentNode); for (int i = 0; i <childcount; i ++) {Object ChildNode = model.getChild (ParentNode, i); if (childnode == 노드) 계속; if (! ispathSelected (parent.pathbyaddingchild (childnode))) false를 반환합니다. } true를 반환합니다. } public void 제거 제거 (Treepath [] Paths) {for (int i = 0; i <paths.length; i ++) {Treepath Path = Paths [i]; if (path.getPathCount () == 1) super.RemovesElectionPaths (new Treepath [] {path}); else tooggleremovesection (Path); }} // 주어진 경로의 조상 노드가 선택되면 선택을 선택하지 않고 주어진 경로와 후손을 제외한 모든 후손을 선택하십시오. // 그렇지 않으면 주어진 경로를 선택하지 않음 개인 무효 공기 토글 레미 모전 (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 (부모); else {super.removeseelectionpaths (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 childcount = model.getChildCount (노드); for (int i = 0; i <childcount; i ++) {Object ChildNode = model.getChild (Node, i); if (childnode! = peeknode) super.addselectionphents (new treepath [] {temp.pathbyaddingchild (childnode)}); }} super.removeseelectionPhents (New Treepath [] {parent}); }} 3. Checktreecellrenderer .java
공개 클래스 checkTreecellrenderer 확장 jpanel 구현 Treecellrenderer {private checkTreesElectionModel SelectionModel; 개인 Treecellrenderer 대의원; // private tristateCheckbox checkbox = new TristateCheckbox (); 개인 jcheckbox checkbox = new Jcheckbox (); PublicTreeCellrenderer (TreeCellrenderer Delegate, CheckTreesElectionModel SelectionModel) {this.delegate = delegate; this.selectionModel = selectionModel; setLayout (New BorderLayout ()); setopaque (false); checkbox.setopaque (false); } 공개 구성 요소 GetTreeCellrendererComponent (jtree tree, 객체 값, 부울 선택, 부울 확장, 부울 잎, int 줄, 부울 hasfocus) {component renderer = delegate.gettreecellrendercomponent (트리, 값, 선택, 확장, 잎, 행); Treepath Path = tree.getPathForly (행); if (path! = null) {system.out.println (path); if (selectionModel.isPathSelected (Path, True)) checkbox.setSelected (true); else {system.out.println (selectionModel.isPartIledsected (PATH)); CheckBox.SetSelected (selectionModel.isPartIledSelected (Path)? true : false); }} removeall (); 추가 (CheckBox, BorderLayout.West); 추가 (Renderer, BorderLayout.Center); 이것을 반환하십시오; }} 4. 사용법
CheckTreemanager CheckTreemanager = New CheckTreemanager (jtree);
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.