How do I set the background color for selected items in a JTree
when using Nimbus L&F ?
The suggestions in old answers to similar questions did not work for Nimbus, so I tried:
final UIDefaults def = new UIDefaults();
def.put( "Tree.background", Color.LIGHT_GRAY );
def.put( "Tree.selectionBackground", Color.RED );
def.put( "Tree.drawHorizontalLines", true );
def.put( "Tree.drawVerticalLines", true );
def.put( "Tree.leftChildIndent", 12 );
def.put( "Tree.rightChildIndent", 12 );
myTree.putClientProperty( "Nimbus.Overrides", def );
myTree.putClientProperty( "Nimbus.Overrides.InheritDefaults", false );
This works for background, drawHorizontalLines
, drawVerticalLines
, leftChildIndent
, and rightChildIndent
, but fails to do anything for the selection background.
I also tried setting various colors for "Tree.textForeground", "Tree.foreground", and "Tree.textBackground", but these had no effect.
I also tried:
final Painter<JComponent> painter = new Painter<JComponent>() {
@Override
public void paint( final Graphics2D g, final JComponent c, final int w, final int h ) {
g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
g.setColor( Color.RED );
g.fillRect( 0, 0, w, h );
}
};
def.put( "Tree:TreeCell[Enabled+Selected].backgroundPainter", painter );
which also had no effect.