本文實例為大家分享了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); //鼠標監(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();
}
}
網(wǎng)頁題目:javaJTreeJCheckBox樹復選框詳解-創(chuàng)新互聯(lián)
文章出自:http://www.chinadenli.net/article8/djojop.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站導航、網(wǎng)頁設計公司、營銷型網(wǎng)站建設、網(wǎng)站制作、響應式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)