Artikel ini membagikan kode spesifik yang ditampilkan di kotak centang Java Jtree Jcheckbox Tree untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1.Checktreemanager.java
Public Class Checktreemanager memperluas mouseadapter mengimplementasikan TreeselectionListener {private checktreeselectionModel selectionModel = null; // pohon jtree pribadi = jtree baru (); pohon jtree pribadi = null; int hotspot = new jcheckbox (). getPreferredSize (). width; checktreemanager publik (pohon jtree) {this.tree = pohon; selectionModel = new checktreeselectionModel (tree.getModel ()); Tree.setCellrenderer (Checktreecellrenderer baru (Tree.getCellrenderer (), selectionModel)); Tree.addmouseListener (ini); // Mouse Mendengarkan SelectionModel.AddtreeselectionListener (ini); // Pilihan Pohon Mendengarkan} public void mouseclicked (mouseEvent me) {treepath path = tree.getPathForlocation (me.getx (), me.gety ()); if (path == null) kembali; if (me.getx ()> tree.getpathbounds (path) .x+hotspot) kembali; boolean dipilih = selectionModel.ispathselected (path, true); selectionModel.removetreeselectionListener (ini); coba {if (dipilih) selectionModel.removeselectionPath (path); lain selectionModel.addselectionPath (Path); } akhirnya {selectionModel.addtreeselectionListener (ini); tree.treedidchange (); }} public checktreeselectionModel getSelectionModel () {return selectionModel; } public void valueChanged (TreeselectionEvent e) {Tree.treedidchange (); }} 2.ChecktreeselectionModel.java
Public Class CheckTreeSelectionModel memperluas defaultTreeselectionModel {Model TreeModel Private; public checktreeselectionModel (model treemoDel) {this.model = model; setSelectionMode (TreeSelectionModel.Discontiguous_tree_selection); } // menguji apakah ada simpul yang tidak dipilih di subtree dari jalur publik yang diberikan boolean dipilih secara terpilih (jalur treepath) {if (isPathSelected (path, true)) mengembalikan false; Treepath [] selectionPaths = getSelectionPaths (); if (selectionPaths == null) return false; untuk (int j = 0; j <selectionpaths.length; j ++) {if (isDescendant (selectionPaths [j], path)) mengembalikan true; } return false; } // Memberitahu apakah jalur yang diberikan dipilih. // Jika Dig benar, maka jalur diasumsikan dipilih, jika // salah satu leluhurnya dipilih. public boolean ispathselected (treepath path, boolean dig) {if (! Dig) mengembalikan super.ispathselected (path); while (path! = null &&! super.ispathselected (path)) path = path.getParentPath (); return path! = null; } // adalah Path1 Keturunan Path2 Private Boolean Isdescendant (Treeepath Path1, Treeepath Path2) {Object Obj1 [] = Path1.getPath (); Objek obj2 [] = path2.getPath (); untuk (int i = 0; i <obj2.length; i ++) {if (obj1 [i]! = obj2 [i]) mengembalikan false; } return true; } public void setSelectionPaths (treepath [] ppaths) {lempar baru unsportedoperationException ("Belum diimplementasikan !!!"); } public void addSelectionPaths (treepath [] Paths) {// Buka semua keturunan jalur [] untuk (int i = 0; i <paths.length; i ++) {treepath path = paths [i]; Treepath [] selectionPaths = getSelectionPaths (); if (selectionPaths == null) break; Arraylist toberemoved = new arraylist (); untuk (int j = 0; j <selectionpaths.length; j ++) {if (isDescendant (selectionPaths [j], path)) toberemoved.add (selectionPaths [j]); } super.removeselectionPaths ((Treeepath []) toberemoved.toarray (treepath baru [0])); } // Jika semua saudara kandung dipilih maka bukalah dan pilih orang tua secara rekursif // Otherwize cukup pilih jalur itu. untuk (int i = 0; i <paths.length; i ++) {jalur treepath = jalur [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 (! isSelectionEmpty ()) RemoveSelectionPaths (getSelectionPaths ()); super.addselectionPaths (treepath baru [] {temp}); }} lain super.addselectionPaths (treepath baru [] {path}); }} // Memberitahu apakah semua saudara kandung dari jalur yang diberikan dipilih. private boolean aresiblingsselected (treepath path) {treepath parent = path.getParentPath (); if (Parent == null) mengembalikan true; Objek node = path.getLastPathComponent (); Objek parentNode = parents.getLastPathComponent (); int childcount = model.getChildCount (parentNode); untuk (int i = 0; i <childCount; i ++) {objek childNode = model.getChild (parentNode, i); if (childnode == node) lanjutkan; if (! isPathSelected (Parent.PathByAddingChild (ChildNode))) Return False; } return true; } public void RemoveSelectionPaths (treepath [] Paths) {for (int i = 0; i <paths.length; i ++) {treepath Path = paths [i]; if (path.getPathCount () == 1) super.removeselectionPaths (treepath baru [] {path}); lain toggleremoveselection (jalur); }} // Jika ada node leluhur dari jalur yang diberikan dipilih maka buka saja itu // dan seleksi semua keturunannya kecuali jalur dan keturunan yang diberikan. // sebaliknya buka saja jalur pribadi yang diberikan void toggleremoveselection (jalur treepath) {stack stack = new stack (); Treeepath Parent = Path.getParentPath (); while (Parent! = null &&! isPathSelected (Parent)) {stack.push (Parent); Parent = Parent.getParentPath (); } if (Parent! = null) stack.push (induk); else {super.removeselectionPaths (treepath baru [] {path}); kembali; } while (! stack.isempty ()) {treepath temp = (treepath) stack.pop (); Treeepath peekpath = stack.isempty ()? Path: (Treeepath) Stack.peek (); Objek node = temp.getLastPathComponent (); Objek peeknode = peekpath.getLastPathComponent (); int childcount = model.getChildCount (node); untuk (int i = 0; i <childcount; i ++) {objek childNode = model.getChild (node, i); if (childnode! = peeknode) super.addselectionPaths (treepath baru [] {temp.pathbyaddingchild (childNode)}); }} super.removeselectionPaths (treepath baru [] {parent}); }} 3.Checktreecellrenderer .java
Public Class Checktreecellrenderer memperluas JPanel mengimplementasikan Treecellrenderer {private checktreeselectionModel selectionModel; Delegasi Private Treecellrenderer; // kotak centang tristatecheckbox pribadi = tristatecheckbox baru (); kotak centang jcheckbox pribadi = jcheckbox baru (); Public Checktreecellrenderer (Delegasi Treecellrenderer, ChecktreeselectionModel SelectionModel) {this.delegate = delegate; this.selectionModel = selectionModel; setLayout (borderlayout baru ()); setOpaque (false); kotak centang.setopaque (false); } komponen publik gettreecellrendererComponent (pohon jtree, nilai objek, boolean dipilih, boolean diperluas, daun boolean, baris int, boolean hasfocus) {component renderer = delegate.gettreecellrendererComponent (value, value, dipilih, diperluas, row, hasfocus); Treepath path = tree.getPathForrow (baris); if (path! = null) {System.out.println (path); if (selectionModel.ispathselected (path, true)) checkbox.setselected (true); else {System.out.println (selectionModel.ispartiyselected (path)); centang kotak. } } Hapus semua(); Tambah (kotak centang, borderlayout.west); add (renderer, borderlayout.center); kembalikan ini; }} 4. Penggunaan
ChecktreeManager checktreeManager = new checktreemanager (jtree);
Di atas adalah semua konten artikel ini. Saya berharap ini akan membantu untuk pembelajaran semua orang dan saya harap semua orang akan lebih mendukung wulin.com.