我有一个 JTree,它的节点是从 DefaultMutableTreeNode 驱动的。每个节点都可以验证或不验证。起初所有节点的图标都是相同的,但是,当我选择它们并按下验证按钮时,我将更改验证节点的图标 。我希望能够在每个节点上单击和写入,因此我不能使用 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;
}
百万谢谢。