I am aware that there are relevant materials exists on google about JTree icons change, but still I am unable to sort out my simple requirements.
I need to change the icon of Root
element of JTree different than it's node
& leaf
.
I manage to change the node & leaf icons, but the default arrow icons (red highlighted in picture) are still there & I have to click on it to drill down the tree whereas I want to click on custom image icons to drill down the tree.
/** CHANGE JTREE DEFAULT ICONS **/
private class userRendererForTree extends DefaultTreeCellRenderer {
private static final long serialVersionUID = 1L;
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);
if (leaf) {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_LEAF));
} else {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_RIGHT));
}
if (expanded) {
setIcon(setImageIcon(AppConstants.BOM_ROUTE_NODE_BOTTOM));
}
return this;
}
}
/** LOAD IMAGE ICON FROM RESOURCES **/
private ImageIcon setImageIcon(String path){
Image image = null;
ImageIcon icon = null;
try {
image = LoadResource.getImageFromResourceAsURL(path);
image = image.getScaledInstance(10, 10, Image.SCALE_SMOOTH);
icon = new ImageIcon(image);
return icon;
} catch (Exception e) {
e.printStackTrace();
}
return icon;
}
Calling of Renderer: -
JTree BomTree = new JTree(createTreeModel());
scrollPane.setColumnHeaderView(BomTree);
BomTree.setCellRenderer(new userRendererForTree());