[英]Change The icon of a certain Node in JTree?
我有一個JTree,它的節點是從DefaultMutableTreeNode驅動的。 每個節點都可以驗證與否。首先,所有節點的圖標都相同,但是當我選擇它們並按下驗證按鈕時,我將更改已驗證節點的ICON 。 我希望能夠在每個節點上單擊並寫入,因此我無法使用JLabel來顯示圖標 。 我做了以下代碼,但它返回NULLException。
class CustomIconRenderer extends DefaultTreeCellRenderer {
ImageIcon defaultIcon;
ImageIcon specialIcon;
ImageIcon closeIcon;
static DefaultTreeModel model;
static myDefaultMutableTreeNode root;
public CustomIconRenderer()
{
openIcon = new ImageIcon(CustomIconRenderer.class.getResource("icons/question.png"));
closeIcon = new ImageIcon(CustomIconRenderer.class.getResource("icons/Target-New-Logo.jpg"));
setLeafIcon(closeIcon);
}
@Override
public Component getTreeCellRendererComponent(JTree tree,Object value,boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
Object nodeObj = ((DefaultMutableTreeNode)value).getUserObject();
Check_each_nodes_are_verified_change_the_icon();
return this;
}
}
public class myDefaultMutableTreeNode extends DefaultMutableTreeNode{
private static int id=0;
private int nodeid;
private int verify;
private int depth;
}
百萬謝謝。
DefaultTreeCellRenderer具有setter,允許設置打開圖標,關閉圖標和葉子圖標。 在重寫的getTreeCellRendererComponent中,在派生的渲染器類中設置這些圖標,然后返回super.getTreeCellRendererComponent返回的圖標。 在渲染之前為每個節點設置時,可以輕松地為特定節點設置一些不同的圖標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.