บทความนี้แชร์รหัสเฉพาะที่แสดงในช่องทำเครื่องหมาย Java JTree JCheckBox Tree สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1.ChecktreeManager.java
Public Class ChecktreeManager ขยาย MouseAdapter ใช้ TreeSelectionListener {Private CheckTreesElectionModel SelectionModel = NULL; // Private JTree Tree = new JTree (); ต้นไม้ jtree ส่วนตัว = null; int hotspot = new jcheckbox (). getPreferredSize (). ความกว้าง; Public ChecktreeManager (JTree Tree) {this.tree = Tree; SelectionModel = ใหม่ CheckTreesElectionModel (tree.getModel ()); Tree.SetCellrenderer (ใหม่ Checktreecellrenderer (tree.getCellrenderer (), SelectionModel)); tree.addmouselistener (นี่); // เมาส์ฟังการเลือก Model.addtreeselectionListener (นี่); // การเลือกต้นไม้การฟัง} โมฆะสาธารณะ mouseclicked (MouseEvent ME) {เส้นทาง treepath = tree.getPathForLocation (me.getx (), me.gety ()); if (path == null) return; if (me.getx ()> tree.getPathbounds (เส้นทาง) .x+ฮอตสปอต) กลับมา; บูลีนเลือก = selectionModel.ispathselected (เส้นทาง, true); SelectionModel.RemovetReesElectionListener (นี้); ลอง {ถ้า (เลือก) selectionModel.RemovesElectionPath (เส้นทาง); else selectionModel.addselectionPath (เส้นทาง); } ในที่สุด {selectionModel.addtreeselectionListener (นี่); Tree.TreedidChange (); }} Public CheckTreesElectionModel getSelectionModel () {return selectionModel; } โมฆะสาธารณะ ValueChanged (TreelectionEvent E) {Tree.TreedIdChange (); - 2.CheckTreesElectionModel.java
Public Class CheckTreesElectionModel ขยาย DEFAELTREESEELECTIONMODEL {โมเดล Treemodel ส่วนตัว; Public CheckTreesElectionModel (Treemodel Model) {this.model = model; setSelectionMode (treelectionModel.discontiguous_tree_selection); } // การทดสอบว่ามีโหนดที่ไม่ได้เลือกใด ๆ ในทรีย่อยของ Path Public Public Boolean isPartialliallySelected (เส้นทาง Treepath) {ถ้า (isPathSelected (path, true)) ส่งคืนเท็จ; Treepath [] SelectionPaths = getSelectionPaths (); if (selectionPaths == null) return false; สำหรับ (int j = 0; j <selectionPaths.length; j ++) {ถ้า (isdescendant (selectionPaths [j], path)) ส่งคืนจริง; } return false; } // บอกว่าเลือกเส้นทางที่กำหนดหรือไม่ // ถ้า DIG เป็นจริงเส้นทางจะถูกเลือกให้เลือกถ้า // หนึ่งในบรรพบุรุษของมันถูกเลือก บูลีนสาธารณะ ispathselected (เส้นทาง Treepath, Boolean Dig) {if (! Dig) return.ispathselected (เส้นทาง); ในขณะที่ (path! = null &&! super.ispathselected (path)) path = path.getParentPath (); เส้นทางกลับ! = null; } // คือ Path1 Descendant ของ Path2 Private Boolean Isdescendant (Treepath Path1, Treepath Path2) {Object obj1 [] = path1.getPath (); วัตถุ obj2 [] = path2.getPath (); สำหรับ (int i = 0; i <obj2.length; i ++) {ถ้า (obj1 [i]! = obj2 [i]) ส่งคืนเท็จ; } return true; } โมฆะสาธารณะ setSelectionPaths (treepath [] pPaths) {โยน unsupportedoperationException ใหม่ ("ยังไม่ได้ดำเนินการ !!!"); } โมฆะสาธารณะ addselectionPaths (treepath [] เส้นทาง) {// ยกเลิกการเลือกลูกหลานทั้งหมดของเส้นทาง [] สำหรับ (int i = 0; i <paths.length; i ++) {เส้นทาง treepath = เส้นทาง [i]; Treepath [] SelectionPaths = getSelectionPaths (); if (selectionPaths == null) break; ArrayList toberemoved = new ArrayList (); สำหรับ (int j = 0; j <selectionPaths.length; j ++) {ถ้า (isdescendant (selectionPaths [j], path)) toberemoved.add (selectionPaths [j]); } super.removeselectionPaths ((treepath []) toberemoved.toarray (ใหม่ treepath [0])); } // ถ้าเลือกพี่น้องทั้งหมดแล้วให้เลือกพวกเขาและเลือกผู้ปกครองซ้ำ // อื่น ๆ เพียงเลือกเส้นทางนั้น สำหรับ (int i = 0; i <paths.length; i ++) {เส้นทาง treepath = paths [i]; Treepath temp = null; ในขณะที่ (aresiblingsselected (path)) {temp = path; if (path.getParentPath () == null) break; path = path.getParentPath (); } if (temp! = null) {ถ้า (temp.getParentPath ()! = null) addSelectionPath (temp.getParentPath ()); else {ถ้า (! isSelectionEmpty ()) ลบออก (getSelectionPaths ()); super.addselectionpaths (ใหม่ treepath [] {temp}); }} else super.addselectionPaths (ใหม่ treepath [] {path}); }} // บอกว่ามีการเลือกพี่น้องทั้งหมดของเส้นทางที่กำหนดหรือไม่ บูลีนส่วนตัว aresiblingsSelected (เส้นทาง treepath) {treepath parent = path.getParentPath (); if (parent == null) return true; Object Node = path.getLastPathComponent (); Object ParentNode = parent.getLastPathComponent (); int childCount = model.getChildCount (ParentNode); สำหรับ (int i = 0; i <childCount; i ++) {Object ChildNode = model.getChild (ParentNode, i); ถ้า (childNode == โหนด) ดำเนินการต่อ; if (! isPathSelected (parent.pathbyaddingChild (เด็ก))) ส่งคืนเท็จ; } return true; } โมฆะสาธารณะลบการถ่ายภาพ (เส้นทาง treepath []) {สำหรับ (int i = 0; i <paths.length; i ++) {เส้นทาง treepath = เส้นทาง [i]; if (path.getPathCount () == 1) super.removeselectionPaths (ใหม่ treepath [] {path}); อื่น TogglerEmoveselection (เส้นทาง); }} // ถ้ามีการเลือกโหนดบรรพบุรุษของเส้นทางที่กำหนดแล้วให้เลือกที่จะเลือก // และเลือกลูกหลานทั้งหมดของมันยกเว้นเส้นทางที่กำหนดและลูกหลาน // มิฉะนั้นเพียงแค่เลือกเส้นทางที่กำหนดเป็นโมฆะส่วนตัว TogglerEmoveselection (เส้นทาง Treepath) {สแต็กสแต็ก = ใหม่สแต็ก (); treepath parent = path.getParentPath (); ในขณะที่ (parent! = null &&! isPathselected (parent)) {stack.push (parent); parent = parent.getParentPath (); } if (parent! = null) stack.push (parent); else {super.removeselectionPaths (ใหม่ treepath [] {path}); กลับ; } ในขณะที่ (! 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 (โหนด); สำหรับ (int i = 0; i <childCount; i ++) {Object ChildNode = model.getChild (Node, i); if (childNode! = peeknode) super.addselectionPaths (ใหม่ treepath [] {temp.pathbyaddingchild (เด็ก)}); }} super.removeselectionPaths (ใหม่ treepath [] {parent}); - 3.Checktreecellrenderer .Java
Public Class ChecktreecellRenderer ขยาย JPanel ใช้ TreeCellRenderer {Private CheckTreesElectionModel SelectionModel; ผู้แทนส่วนตัว Treecellrenderer; // ช่องทำเครื่องหมายส่วนตัว tristateCheckbox = ใหม่ tristateCheckbox (); ช่องทำเครื่องหมายส่วนตัว jcheckbox = new jcheckbox (); Public Checktreecellrenderer (Treecellrenderer Delegate, CheckTreesElectionModel SelectionModel) {this.delegate = มอบหมาย; this.selectionModel = selectionModel; SetLayout (New BorderLayout ()); Setopaque (เท็จ); ช่องทำเครื่องหมาย SetOpaque (เท็จ); } ส่วนประกอบสาธารณะ getTreecellRendererComponent (ต้นไม้ jtree, ค่าวัตถุ, บูลีนที่เลือก, บูลีนขยาย, ใบบูลีน, แถวแถว, บูลีน hasfocus) {ส่วนประกอบ renderer = delegate.getTreecellRendererComponent Treepath Path = tree.getPathForrow (แถว); if (path! = null) {system.out.println (พา ธ ); if (selectionModel.ispathselected (path, true)) ช่องทำเครื่องหมายที่ SetSetelected (จริง); else {system.out.println (selectionModel.ispartiallySelected (เส้นทาง)); ช่องทำเครื่องหมายที่เลือก (selectionModel.ispartiallySelected (เส้นทาง)? true: false); }} removeAll (); เพิ่ม (ช่องทำเครื่องหมาย, BorderLayout.west); เพิ่ม (Renderer, BorderLayout.Center); คืนสิ่งนี้; - 4. การใช้งาน
checktreeManager checktreeManager = ใหม่ checktreeManager (jtree);
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น