网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

javaJTreeJCheckBox树复选框详解

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

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);  

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


网页题目:javaJTreeJCheckBox树复选框详解
网站地址:http://bjjierui.cn/article/geshhj.html

其他资讯