Cet article partage le code spécifique affiché dans la case Java JTree JCheckbox Tree pour votre référence. Le contenu spécifique est le suivant
1.ChecktreManager.java
Classe publique CheckTreEmanager étend MousEadapter implémente TreeSelectionListener {private checkTreeSelectionModel SELECTIONMODEL = NULL; // private jtree arbre = new Jtree (); Jtree Tree privé = null; int hotspot = new JCheckbox (). getPreferRedSize (). largeur; public CheckTreEmanager (Jtree Tree) {this.tree = arbre; SELECTIONMODEL = NOUVEAU CHECKETREEELECTIONMODEL (Tree.GetModel ()); Tree.SetCellRenderer (new CheckTreeCellRenderer (Tree.getCellRenderer (), SELECTIONMODEL)); Tree.AddMouseListener (this); // Souris d'écoute SELECTIONMODEL.AddtreeSelectionListener (This); // Sélection des arbres Écoute} public void Mouseclicked (Mouseevent Me) {Treepath Path = Tree.getPathForLocation (me.getx (), me.gety ()); if (path == null) return; if (me.getx ()> arbre.getPathBounds (path) .x + hotspot) return; Boolean Selected = SelectionModel.ispathSelected (Path, True); SELECTIONMODEL.REMOVETREEELECLECTIONNETER (This); essayez {if (sélectionné) selectionModel.RemoveselelectionPath (path); else selectionModel.AddDelectionPath (path); } enfin {selectionmodel.addtreeSelectionListener (this); Tree.TreedIdChange (); }} public checkTreeSelectionModel getSelectionModel () {return selectionModel; } public Void ValueChanged (TreeselectionEvent E) {Tree.TreedIdChange (); }} 2.Checktreeselectionmodel.java
classe publique CheckTreeSelectionModel étend defaultTreeSelectionModel {Private Treemodel modèle; public CheckTreeSelectionModel (modèle Treemodel) {this.model = modèle; setSelectionMode (arreselectionmodel.discontigoust_tree_selection); } // teste s'il y a un nœud non sélectionné dans le sous-arbre du chemin donné public booléen ispartisestyselected (Treepath Path) {if (isPathSelected (path, true)) return false; Treepath [] selection-paths = getSelectionPaths (); if (selectionpaths == null) renvoie false; pour (int j = 0; j <selectionPaths.Length; j ++) {if (isDescendant (SelectionPaths [j], path)) return true; } return false; } // indique si le chemin donné est sélectionné. // Si la fouille est vraie, alors un chemin est supposé être sélectionné, si // l'un de ses ancêtres est sélectionné. public booléen isPathSelected (Treepath Path, boolean dig) {if (! dig) return super.ispathselected (path); while (path! = null &&! super.ispathSelected (path)) path = path.getParentPath (); chemin de retour! = null; } // est le descendant PATH1 de Path2 Private Boolean Isdenshenant (Treepath Path1, Treepath Path2) {objet obj1 [] = path1.getPath (); Objet obj2 [] = path2.getPath (); for (int i = 0; i <obj2.Length; i ++) {if (obj1 [i]! = Obj2 [i]) return false; } return true; } public void SetSelectionPaths (Treepath [] ppaths) {lancez un nouveau non soutenue par une conception ("non implémenté !!!"); } public void addSelectionPaths (Treepath [] paths) {// Unselect Tous les descendants de chemins [] pour (int i = 0; i <paths.length; i ++) {Treepath path = paths [i]; Treepath [] selection-paths = getSelectionPaths (); if (selectionpaths == null) casser; ArrayList toberEMoved = new ArrayList (); pour (int j = 0; j <selectionPaths.length; j ++) {if (isDescendant (selectionPaths [j], path)) tabereMoved.add (sectionpaths [j]); } super.reMoveselelectionPaths ((Treepath []) ToberEmoved.toArray (new Treepath [0])); } // Si tous les frères et sœurs sont sélectionnés, ne les sélectionnez pas et sélectionnez Parent récursivement // Ensemblewize, sélectionnez simplement ce chemin. for (int i = 0; i <paths.length; i ++) {Treepath path = paths [i]; Treepath temp = null; while (aresiblingsSelected (path)) {temp = path; if (path.getParentPath () == null) pause; path = path.getParentPath (); } if (temp! = null) {if (temp.getParentPath ()! = null) addSelectionPath (temp.getParentPath ()); else {if (! IsselectionEmpty ()) supprime-élément (getSelectionPaths ()); super.adddselectionPaths (nouveau Treepath [] {temp}); }} else super.adddSelectionPaths (new Treepath [] {path}); }} // indique si tous les frères et sœurs du chemin donné sont sélectionnés. Boolean privé aresiblingsSelected (Treepath Path) {Treepath Parent = path.getParentPath (); if (parent == null) renvoie true; Objet node = path.getlastPathComponent (); Objet parentNode = parent.getlastPathComponent (); int childCount = Model.getChildCount (parentNode); for (int i = 0; i <childCount; i ++) {objet childNode = Model.getchild (parentNode, i); if (childNode == nœud) continue; if (! IspathSelected (parent.pathByAddingChild (childNode))) renvoie false; } return true; } public void reloveselectionPaths (Treepath [] paths) {for (int i = 0; i <paths.length; i ++) {Treepath path = paths [i]; if (path.getPathCount () == 1) super.reMoveselelectionPaths (new Treepath [] {path}); else togglereMoveselelection (chemin); }} // Si un nœud d'ancêtre du chemin donné est sélectionné, il ne le sait // et sélectionne tous ses descendants sauf le chemin donné et les descendants. // Sinon, il suffit de désélectionner le chemin donné privé void toggleRemoveSelection (Treepath Path) {stack stack = new Stack (); Treepath Parent = Path.GetParentPath (); while (parent! = null &&! isPathSelected (parent)) {stack.push (parent); parent = parent.getParentPath (); } if (parent! = null) stack.push (parent); else {super.reMoveselelectionPaths (new Treepath [] {path}); retour; } while (! stack.isempty ()) {Treepath temp = (Treepath) stack.pop (); Treepath PeekPath = stack.iSempty ()? Chemin: (Treepath) Stack.Peek (); Objet node = temp.getlastPathComponent (); Objet peekNode = peekpath.getlastPathComponent (); int childCount = Model.getChildCount (nœud); for (int i = 0; i <childCount; i ++) {objet childNode = Model.getChild (node, i); if (childNode! = peeknode) super.adddselectionPaths (new Treepath [] {temp.pathByAddingChild (childNode)}); }} super.reMoveselelectionPaths (new Treepath [] {parent}); }} 3.ChecktreeCellRenderer .java
Classe publique CheckTreeCellRenderer étend JPanel implémente TreeCellRenderer {private checkTreeSelectionModel SelectionModel; Délégué privé de TreecellRenderer; // private tristateCheckbox checkbox = new TristateCheckBox (); private jCheckbox checkbox = new JCheckBox (); Public CheckTreeCellRenderer (TreeCellRenderer Delegate, CheckTreeSelectionModel SelectionModel) {this.delegate = délégué; this.selectionmodel = selectionModel; setLayout (new BorderLayout ()); setOpaque (false); checkbox.setOpaque (false); } composant public getTreeCellRenderComponent (arbre Jtree, valeur d'objet, booléen sélectionné, boolean étendu, feuille booléenne, ligne int, booléen hasfocus) {composant rendu = legate.gettreeCellRendeRerComponent (arbre, valeur, sélectionnée, étendue, feuille, ligne, hasfocus); Treepath Path = Tree.getPathForrow (Row); if (path! = null) {System.out.println (path); if (selectionmodel.ispathSelected (path, true)) checkbox.setSelected (true); else {System.out.println (SelectionModel.ispartialSelected (path)); Checkbox.SetSelected (SELECTIONMODEL.ispArtiseLelected (Path)? True: FALSE); }} removeAll (); Add (Checkbox, BorderLayout.West); Add (rendu, borderlayout.center); retourner ceci; }} 4. Utilisation
CheckTreEManager CheckTreEmanager = New CheckTreeManager (Jtree);
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.