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://azwzsj.com/article/geshhj.html