Kallikantzaros
Kallikantzaros

Reputation: 139

Custom node icon for JTree when disabled

I have a JTree in which I am displaying different icons according to a nodes depth in the tree, which I have done using the following code:

public Component getTreeCellRendererComponent(JTree tree,
        Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus){

        super.getTreeCellRendererComponent(tree, value,
        selected, expanded, leaf, row, hasFocus);

        int level = ((DefaultMutableTreeNode) value).getLevel());
        JLabel label = (JLabel) this ;
        if(level == 0) {
            label.setIcon( new ImageIcon("icon1.gif") ) ;
        }else if(level == 1) {
            label.setIcon( new ImageIcon("icon2.png") ) ;
        }
        return this;
    }

This works as I would expect, except that if the JTree component is disabled, the icons revert to the (disabled) versions of the defaults. Do I need to create disabled versions of my icons, and if, so, how are they set using this method. I tried adding:

if(!tree.isEnabled()) {
    label.setIcon( new ImageIcon("icon1_disabled.gif") ) ;
}

... but I am still left with the defaults in a disabled component. Any hints in the right direction would be appreciated.

Upvotes: 4

Views: 839

Answers (1)

Kevin K
Kevin K

Reputation: 9584

Use label.setDisabledIcon(new ImageIcon("icon1_disabled.gif"));. I tested it and seems to work fine.

Upvotes: 4

Related Questions