欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java JTree JCheckBox树复选框详解

程序员文章站 2024-04-01 18:16:16
本文实例为大家分享了java jtree jcheckbox树复选框展示的具体代码,供大家参考,具体内容如下 1.checktreemanager.java...

本文实例为大家分享了java jtree jcheckbox树复选框展示的具体代码,供大家参考,具体内容如下

1.checktreemanager.java

public class checktreemanager extends mouseadapter implements treeselectionlistener 
{ 
 
  private checktreeselectionmodel selectionmodel = null; 
  
// private jtree tree = new jtree(); 
  private jtree tree = null; 
  
  int hotspot = new jcheckbox().getpreferredsize().width; 
  
  public checktreemanager(jtree tree) 
  { 
   this.tree = tree; 
   selectionmodel = new checktreeselectionmodel(tree.getmodel()); 
   tree.setcellrenderer(new checktreecellrenderer(tree.getcellrenderer(), selectionmodel)); 
   tree.addmouselistener(this); //鼠标监听 
   selectionmodel.addtreeselectionlistener(this); //树选择监听 
  } 
  
  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 selected = selectionmodel.ispathselected(path, true); 
   selectionmodel.removetreeselectionlistener(this); 
   
   try 
   { 
    if(selected) 
     selectionmodel.removeselectionpath(path); 
    else 
     selectionmodel.addselectionpath(path); 
   } 
   finally 
   { 
    selectionmodel.addtreeselectionlistener(this); 
    tree.treedidchange(); 
   } 
  } 
  
  public checktreeselectionmodel getselectionmodel() 
  { 
   return selectionmodel; 
  } 
  
  public void valuechanged(treeselectionevent e) 
  { 
   tree.treedidchange(); 
  } 
  
} 

2.checktreeselectionmodel.java

public class checktreeselectionmodel extends defaulttreeselectionmodel 
{ 
  private treemodel model; 
  
  public checktreeselectionmodel(treemodel model) 
  { 
   this.model = model; 
   setselectionmode(treeselectionmodel.discontiguous_tree_selection); 
  } 
  
  // tests whether there is any unselected node in the subtree of given path 
  public boolean ispartiallyselected(treepath path){ 
   if(ispathselected(path, true)) 
    return false; 
   treepath[] selectionpaths = getselectionpaths(); 
   if(selectionpaths==null) 
    return false; 
   for(int j = 0; j<selectionpaths.length; j++) 
   { 
    if(isdescendant(selectionpaths[j], path)) 
     return true; 
   } 
   return false; 
  } 
  
  // tells whether given path is selected. 
  // if dig is true, then a path is assumed to be selected, if 
  // one of its ancestor is selected. 
  public boolean ispathselected(treepath path, boolean dig) 
  { 
   if(!dig) 
    return super.ispathselected(path); 
   while(path!=null && !super.ispathselected(path)) 
    path = path.getparentpath(); 
   return path!=null; 
  } 
  
  // is path1 descendant of path2 
  private boolean isdescendant(treepath path1, treepath path2) 
  { 
   object obj1[] = path1.getpath(); 
   object 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) 
  { 
   throw new unsupportedoperationexception("not implemented yet!!!"); 
  } 
  
  public void addselectionpaths(treepath[] paths) 
  { 
   // unselect all descendants of paths[] 
   for(int i = 0; i<paths.length; i++){ 
    treepath path = paths[i]; 
    treepath[] selectionpaths = getselectionpaths(); 
    if(selectionpaths==null) 
     break; 
    arraylist toberemoved = new arraylist(); 
    for(int j = 0; j<selectionpaths.length; j++) 
    { 
     if(isdescendant(selectionpaths[j], path)) 
      toberemoved.add(selectionpaths[j]); 
    } 
    super.removeselectionpaths((treepath[])toberemoved.toarray(new treepath[0])); 
   } 
   
   // if all siblings are selected then unselect them and select parent recursively 
   // otherwize just select that path. 
   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()) 
       removeselectionpaths(getselectionpaths()); 
      super.addselectionpaths(new treepath[]{temp}); 
     } 
    } 
    else 
     super.addselectionpaths(new treepath[]{ path}); 
   } 
  } 
  
  // tells whether all siblings of given path are selected. 
  private boolean aresiblingsselected(treepath path) 
  { 
   treepath parent = path.getparentpath(); 
   if(parent==null) 
    return 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==node) 
     continue; 
    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(new treepath[]{ path}); 
    else 
     toggleremoveselection(path); 
   } 
  } 
  
  // if any ancestor node of given path is selected then unselect it 
  // and selection all its descendants except given path and descendants. 
  // otherwise just unselect the given path 
  private 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.removeselectionpaths(new treepath[]{path}); 
    return; 
   } 
   
   while(!stack.isempty()) 
   { 
    treepath temp = (treepath)stack.pop(); 
    treepath peekpath = stack.isempty() ? path : (treepath)stack.peek(); 
    object node = temp.getlastpathcomponent(); 
    object peeknode = peekpath.getlastpathcomponent(); 
    int childcount = model.getchildcount(node); 
    for(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

public class checktreecellrenderer extends jpanel implements treecellrenderer 
{ 
 private checktreeselectionmodel selectionmodel; 
 private treecellrenderer delegate; 
// private tristatecheckbox checkbox = new tristatecheckbox(); 
 private jcheckbox checkbox = new jcheckbox(); 
 
 public checktreecellrenderer(treecellrenderer delegate, checktreeselectionmodel selectionmodel){ 
  this.delegate = delegate; 
  this.selectionmodel = selectionmodel; 
  setlayout(new borderlayout()); 
  setopaque(false); 
  checkbox.setopaque(false); 
 } 
 
 
 public component gettreecellrenderercomponent(jtree tree, object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasfocus){ 
  component renderer = delegate.gettreecellrenderercomponent(tree, value, selected, expanded, leaf, row, 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.ispartiallyselected(path)); 
    checkbox.setselected(selectionmodel.ispartiallyselected(path) ? true : false); 
   } 
  } 
  removeall(); 
  add(checkbox, borderlayout.west); 
  add(renderer, borderlayout.center); 
  return this; 
 } 
} 

4.用法

checktreemanager checktreemanager = new checktreemanager(jtree);  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。