تشارك هذه المقالة الرمز المحدد المعروض في خانة الاختيار Java Jtree Jcheckbox Tree للرجوع إليها. المحتوى المحدد كما يلي
1.Checktreemanager.java
يمتد الفئة العامة CheckTreemanager MouseadApter تنفذ TreeselectionListener {private CheckTreeSelectionModel SelectionModel = null ؛ // private jtree tree = new Jtree () ؛ شجرة jtree الخاصة = فارغة ؛ int hotspot = new jCheckBox (). getPreferRedSize (). العرض ؛ public checkTreemanager (jtree tree) {this.tree = tree ؛ selectModel = جديد checkTreeSelectionModel (tree.getModel ()) ؛ Tree.setCellRenderer (CheckTreecellRenderer جديد (Tree.getCellRenderer () ، SelectModel)) ؛ tree.addmouselistener (هذا) ؛ // mouse الاستماع selectmodel.addtreeselectionListener (هذا) ؛ // اختيار شجرة الاستماع} 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 Selection = SelectionModel.ispathselected (path ، true) ؛ SelectionModel.RemovetReesElectionListener (هذا) ؛ حاول {if (محدد) selectModel.RemovesElectionPath (path) ؛ selectionmodel.addselectionpath (path) ؛ } أخيرًا {selectModel.AddTreeSelectionListener (this) ؛ tree.treedIdChange () ؛ }} public checkTreSelectionModel getSeSeSeSelectionModel () {return selectModel ؛ } public void valuechanged (treeselectionevent e) {tree.treedIdChange () ؛ }} 2.CheckTreeSelectionModel.Java
الطبقة العامة CheckTreeSelectionModel يمتد DefaultTreesElectionModel {Private Treemodel Model ؛ checkTreeSelectionModel (نموذج treemodel) {this.model = model ؛ setSelectionMode (TreeSelectionModel.discontigous_tree_selection) ؛ } // اختبارات ما إذا كان هناك أي عقدة غير محددة في الشجرة الفرعية للمسار المعطى المنطقي المنطقي Isparticly (Treepath Path) {if (isPathected (path ، true)) return false ؛ treepath [] selectPaths = getSelectionPaths () ؛ if (selectPaths == NULL) إرجاع خطأ ؛ لـ (int j = 0 ؛ j <selectPaths.length ؛ j ++) {if (isDescendant (selectPaths [j] ، path)) return true ؛ } إرجاع خطأ ؛ } // يخبرنا ما إذا كان يتم تحديد المسار المعطى. // إذا كان الحفر صحيحًا ، فمن المفترض أن يتم اختيار المسار ، إذا تم تحديد // أحد أسلافه. Boolean public Ispathseced (Treepath Path ، Boolean Dig) {if (! dig) return super.ispathselected (path) ؛ بينما (المسار! = null &&! super.ispathselected (path)) path = path.getParentPath () ؛ مسار العودة! = فارغة ؛ } // هو سليل المسار 1 من isdescendant boolean path2 (trepeath path1 ، treepath path2) {object obj1 [] = path1.getPath () ؛ كائن OBJ2 [] = path2.getPath () ؛ لـ (int i = 0 ؛ i <obj2.length ؛ i ++) {if (obj1 [i]! = obj2 [i]) return false ؛ } إعادة صواب ؛ } public void setSelectionPaths (treepath [] ppaths) {رمي جديد غير مدعوم جديد ("لم يتم تنفيذه بعد !!!) ؛ } public void addelypaths (treepath [] مسارات) {// إلغاء تحديد جميع أحفاد المسارات [] لـ (int i = 0 ؛ i <paths.length ؛ i ++) {treepath path = paths [i] ؛ treepath [] selectPaths = getSelectionPaths () ؛ if (selectPaths == null) break ؛ ArrayList toberemoved = new ArrayList () ؛ لـ (int j = 0 ؛ j <selectPaths.length ؛ j ++) {if (isDescendant (selectPaths [j] ، path)) teberemoved.add (selectPaths [j]) ؛ } super.removeselectionpaths ((treepath []) teberemoved.toarray (New Treepath [0])) ؛ } // إذا تم تحديد جميع الأشقاء ، فقم بإلغاء تحديدها وحدد الأصل بشكل متكرر // otherwize فقط حدد هذا المسار. لـ (int i = 0 ؛ i <paths.length ؛ i ++) {treepath path = paths [i] ؛ Treepath temp = null ؛ بينما (aresiblingsselected (path)) {temp = path ؛ if (path.getParentPath () == null) break ؛ path = path.getParentPath () ؛ } if (temp! = null) {if (temp.getParentPath ()! = null) addSelectionPath (temp.getParentPath ()) ؛ آخر {if (! isselectionEmpty ()) removeselectionpaths (getSelectionPaths ()) ؛ Super.AddSelectionPaths (New Treepath [] {temp}) ؛ }} else super.addselectionPaths (treepath جديد [] {path}) ؛ }} // يخبرنا ما إذا كان يتم اختيار جميع الأشقاء من المسار المعطى. private boolean aresiblingsselected (treepath path) {treepath parent = path.getParentPath () ؛ إذا (الوالد == فارغ) إرجاع صحيح ؛ Object Node = path.getLastPathComponent () ؛ كائن parentNode = parent.getLastPathComponent () ؛ int childcount = model.getChildCount (parentNode) ؛ لـ (int i = 0 ؛ i <childcount ؛ i ++) {object childnode = model.getChild (parentNode ، i) ؛ إذا (childnode == node) تابع ؛ if (! isPathected (parent.pathbyadddingchild (childnode))) إرجاع خطأ ؛ } إعادة صواب ؛ } public void removeselectionpaths (treepath [] مسارات) {for (int i = 0 ؛ i <paths.length ؛ i ++) {treepath path = paths [i] ؛ if (path.getPathCount () == 1) super.removeselectionPaths (treepath جديد [] {path}) ؛ Else toggleremoveselection (path) ؛ }} // إذا تم تحديد أي عقدة سلف من المسار المعطى ، ثم قم بإلغاء تحديده // واختيار جميع أحفادها باستثناء المسار المعطى والأحفاد. // وإلا فقط قم بإلغاء تحديد المسار المعطى بتبادل الفراغ الخاص (مسار TreePath) {stack stack = new stack () ؛ treepath parent = path.getParentPath () ؛ بينما (الأصل! = null &&! isPathselected (الوالد)) {stack.push (parent) ؛ parent = parent.getParentPath () ؛ } if (parent! = null) stack.push (parent) ؛ else {super.removeselectionPaths (New Treepath [] {path}) ؛ يعود؛ } بينما (! stack.isempty ()) {treepath temp = (treepath) stack.pop () ؛ Treepath Peekpath = stack.isempty ()؟ المسار: (treepath) stack.peek () ؛ Object Node = temp.getLastPathComponent () ؛ كائن peeknode = peekpath.getlastpathcomponent () ؛ int childcount = model.getChildCount (node) ؛ لـ (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.ChecktreecellRenderer .Java
الطبقة العامة chectTreEcellRenderer يمتد JPanel الأدوات treecellrenderer {private checkTreeselectionModel selectModel ؛ مندوب خاص TreecellRenderer ؛ // مربع اختيار TristateCheckbox الخاص = جديد tristatecheckbox () ؛ مربع اختيار jCheckbox الخاص = new jCheckbox () ؛ Public CheckTreeCellRenderer (TreecellRenderer Pepegate ، CheckTreeSelectionModel SelectionModel) {this.delegate = depegate ؛ this.selectionModel = SelectionModel ؛ setLayout (New BorderLayout ()) ؛ setopaque (خطأ) ؛ checkbox.setopaque (false) ؛ } المكون العام getTreEcellRendererComponent (شجرة Jtree ، قيمة الكائن ، المحدد المنطقي ، موسع منطقي ، أوراق منطقية ، int الصف ، hasfocus المنطقي) {Component Renderer = depegate.getTreecellRenderence (شجرة ، القيمة ، تم تحديدها ، ورقة ، صف ، hasfocus) ؛ treepath path = tree.getPathforrow (row) ؛ if (path! = null) {system.out.println (path) ؛ if (selectModel.ispathselected (path ، true)) مربع الاختيار. else {system.out.println (selectModel.ispartily selected (path)) ؛ مربع الاختيار. }} removeall () ؛ إضافة (مربع الاختيار ، borderlayout.west) ؛ إضافة (العارض ، borderlayout.center) ؛ إرجاع هذا ؛ }} 4. الاستخدام
CheckTreemanager CheckTreemanager = جديد CheckTreemanager (Jtree) ؛
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.