But I can not set custom open/close
In your TreeCellRenderer
, you can use setOpenIcon()
and setClosedIcon()
as required in conjunction with the defined parameters and predicates related to your model. Given a tree having the default JTree
model, the TreeRenderer
below will use the closed
and open
icons for the sports
node:
private static class TreeRenderer extends DefaultTreeCellRenderer {
private static final Icon closed =
(Icon) UIManager.get("InternalFrame.maximizeIcon");
private static final Icon open =
(Icon) UIManager.get("InternalFrame.minimizeIcon");
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean exp, boolean leaf, int row, boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
String s = node.getUserObject().toString();
if ("sports".equals(s)) {
setOpenIcon(open);
setClosedIcon(closed);
} else {
setOpenIcon(getDefaultOpenIcon());
setClosedIcon(getDefaultClosedIcon());
}
super.getTreeCellRendererComponent(
tree, value, sel, exp, leaf, row, hasFocus);
return this;
}
}
See also this related example.