JLabel as the cell renderer, with the proper background color set. Nothing.The answer to the mystery is the fact that
JLabel is transparent by default, transparent components don't render their background, and DefaultTreeCellRenderer extends JLabel. JComponent.setOpaque() javadocs:/**The workaround? Add
* If true the component paints every pixel within its bounds.
* Otherwise, the component may not paint some or all of its
* pixels, allowing the underlying pixels to show through.
*
* The default value of this property is false forJComponent.
* However, the default value for this property on most standard
*JComponentsubclasses (such asJButtonand
*JTree) is look-and-feel dependent.
*
* @param isOpaque true if this component should be opaque
* @see #isOpaque
* @beaninfo
* bound: true
* expert: true
* description: The component's opacity
*/
this.setOpaque(true) in the overriden getTreeCellRendererComponent() method:
public class BagroundColorEnabledRenderer extends DefaultTreeCellRenderer {
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);
this.setOpaque(true);
return this;
}
}
Happy Coding!
Leave a comment