最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java JTree JCheckBox樹復(fù)選框詳解

 更新時(shí)間:2017年11月23日 11:40:04   作者:yyyzlf  
這篇文章主要為大家詳細(xì)介紹了java JTree JCheckBox樹復(fù)選框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java JTree JCheckBox樹復(fù)選框展示的具體代碼,供大家參考,具體內(nèi)容如下

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); //鼠標(biāo)監(jiān)聽 
   selectionModel.addTreeSelectionListener(this); //樹選擇監(jiān)聽 
  } 
  
  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);  

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mybatis的update更新批量與普通解決方式對比

    Mybatis的update更新批量與普通解決方式對比

    這篇文章主要為大家介紹了Mybatis的update更新批量與普通解決方式對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • Java利用Selenium操作瀏覽器的示例詳解

    Java利用Selenium操作瀏覽器的示例詳解

    本文主要介紹如何使用java代碼利用Selenium操作瀏覽器,某些網(wǎng)頁元素加載慢,如何操作元素就會(huì)把找不到元素的異常,此時(shí)需要設(shè)置元素等待,等待元素加載完,再操作,感興趣的可以了解一下
    2023-01-01
  • java JTree JCheckBox樹復(fù)選框詳解

    java JTree JCheckBox樹復(fù)選框詳解

    這篇文章主要為大家詳細(xì)介紹了java JTree JCheckBox樹復(fù)選框的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    一文搞懂Mybatis中Mapper配置文件獲取參數(shù)的五種方式

    這篇文章主要介紹了Mybatis中Mapper配置文件獲取參數(shù)的五種方式,文中通過代碼示例講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-03-03
  • 自定義BufferedReader的實(shí)例

    自定義BufferedReader的實(shí)例

    下面小編就為大家分享一篇自定義BufferedReader的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • java字符串切割實(shí)例學(xué)習(xí)(獲取文件名)

    java字符串切割實(shí)例學(xué)習(xí)(獲取文件名)

    在Java中處理一些路徑相關(guān)的問題的時(shí)候,如要取出ie瀏覽器上傳文件的文件名,由于ie會(huì)把整個(gè)文件路徑都作為文件名上傳,需要用java.lang.String中的replaceAll或者split來處理,下面看看使用方法
    2013-12-12
  • java高并發(fā)情況下高效的隨機(jī)數(shù)生成器

    java高并發(fā)情況下高效的隨機(jī)數(shù)生成器

    這篇文章主要介紹了java高并發(fā)情況下高效的隨機(jī)數(shù)生成器,對于性能有要求的同學(xué),可以參考下
    2021-04-04
  • java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫

    java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫

    前面給大家介紹過使用javascript,php,c#,python等語言實(shí)現(xiàn)人民幣大寫格式化,這篇文章主要介紹了java實(shí)現(xiàn)將數(shù)字轉(zhuǎn)換成人民幣大寫的代碼,非常的簡單實(shí)用,分享給大家,需要的朋友可以參考下
    2015-04-04
  • 阿里Sentinel支持Spring Cloud Gateway的實(shí)現(xiàn)

    阿里Sentinel支持Spring Cloud Gateway的實(shí)現(xiàn)

    這篇文章主要介紹了阿里Sentinel支持Spring Cloud Gateway的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-04-04
  • MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)

    MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)

    本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn),主要包括單個(gè)參數(shù)傳遞綁定,序號(hào)參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12

最新評(píng)論

南平市| 体育| 荔波县| 原平市| 西宁市| 达州市| 嵊泗县| 沐川县| 苗栗县| 蕉岭县| 阿城市| 旌德县| 赤水市| 阿拉尔市| 长岭县| 大邑县| 平谷区| 阳高县| 历史| 中超| 双江| 福清市| 南城县| 宁远县| 青海省| 呼玛县| 仪征市| 吕梁市| 收藏| 金川县| 衡东县| 营山县| 玉山县| 朝阳市| 鄄城县| 大姚县| 贵德县| 天水市| 邯郸县| 盈江县| 栖霞市|