Use the approach shown in TreeIconDemo2
to condition the renderer based on the model's value. For example,
private class MyRenderer extends DefaultTreeCellRenderer {
private Icon okIcon;
public MyRenderer(Icon okIcon) {
this.okIcon = okIcon;
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean exp, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel, exp, leaf, row, hasFocus);
YourMutableTreeNode node = (YourMutableTreeNode) value;
if (leaf && node.getStatus().equals("OK")) {
setIcon(okIcon);
}
return this;
}
}
Addendum: You can't simply invoke nodeChanged()
on a new TreeNode
that's not part of the tree; the new node has no parent. If you specify an existing node to nodeChanged()
, the notification will happen automatically. If needed, there's an example of searching a tree here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…