java JTree JCheckBox树复选框详解

 更新时间:2017年11月23日 11:40:04   作者:yyyzlf  
这篇文章主要为大家详细介绍了java JTree JCheckBox树复选框的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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);  

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

相关文章

  • java捕获异常信息存入txt文件示例

    java捕获异常信息存入txt文件示例

    这篇文章主要介绍了java捕获异常信息存入txt文件示例,需要的朋友可以参考下
    2014-04-04
  • C++字符串的处理详解

    C++字符串的处理详解

    这篇文章主要介绍了C++ string字符串类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-08-08
  • RocketMQ 源码分析Broker消息刷盘服务

    RocketMQ 源码分析Broker消息刷盘服务

    这篇文章主要为大家介绍了RocketMQ 源码分析Broker消息刷盘服务示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • springboot中mybatis多数据源动态切换实现

    springboot中mybatis多数据源动态切换实现

    在开发中,动态数据源配置还是用的比较多的,比如在多数据源使用方面,又或者是在多个DB之间切换方面。这里给出一个动态数据源的配置方案,感兴趣的可以了解一下
    2021-07-07
  • 详解Java基础之封装

    详解Java基础之封装

    这篇文章主要为大家介绍了Java基础之封装,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • Java基于中介者模式实现多人聊天室功能示例

    Java基于中介者模式实现多人聊天室功能示例

    这篇文章主要介绍了Java基于中介者模式实现多人聊天室功能,详细分析了中介者模式的概念、原理以及使用中介模式实现多人聊天的步骤、操作技巧与注意事项,需要的朋友可以参考下
    2018-05-05
  • Java数据结构之单链表详解

    Java数据结构之单链表详解

    在之前的学习中,我们主要了解了很多 Java 的 基本语法,但是在之后的 Java学习中,了解基础数据结构的知识非常重要,数据结构的思想可以帮助我们更加清晰明白的了解 Java 的解题思路等等.今天我们就来开始学习实现一个Java基础的单链表,需要的朋友可以参考下
    2021-05-05
  • Java遍历Map四种方式讲解

    Java遍历Map四种方式讲解

    这篇文章主要介绍了Java遍历Map四种方式讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java实现的数组去重与排序操作详解

    Java实现的数组去重与排序操作详解

    这篇文章主要介绍了Java实现的数组去重与排序操作,结合实例形式分析了Java针对数组去重及排序操作相关遍历、排序、判断等使用技巧与注意事项,需要的朋友可以参考下
    2018-07-07
  • Spring @Cacheable注解类内部调用失效的解决方案

    Spring @Cacheable注解类内部调用失效的解决方案

    这篇文章主要介绍了Spring @Cacheable注解类内部调用失效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01

最新评论