Is This a Nimbus Bug?
This code uses Java 6u11 with the Nimbus Look and Feel. I know how to create and apply custom Painters, but I’d rather avoid any com.sun.* dependencies. Those package names will change in Java 7, so I’d rather keep my code “clean”.
Case 1: JButton Background Color
This works:
JButton btn = new JButton("red");
btn.setBackground(Color.RED);
As expected, the button paints with a red background. That’s because deep inside com.sun.java.swing.plaf.nimbus.ButtonPainter, they override getExtendedCacheKeys(JComponent c). In that method, the painter asks the component for its background, on line 398:
if ("background".equals(property)) {
color = c.getBackground();
}
Case 2: JButtons in a JToolBar
When you put buttons into a JToolBar, setBackground() no longer has any effect. That’s because ToolBarButtonPainter fails to override getExtendedCacheKeys(). ToolBarButtonPainter also has a private array named componentColors like ButtonPainter, but the array is not used.
As I see it, there is no way to set the background color on buttons in a JToolBar without introducing com.sun.* dependencies in our code.
Is this a bug, or a feature? It seems awfully convenient to simply call setBackground(...), even if we’re using Nimbus painters behind the scenes.
Hey,
Can you point me to some verification that “Those package names will change in Java 7″?
I have to work with some code that unfortunately relies on a dependency on com.sun class. If I can prove that this will break in java 7, then I may be able to get my boss to devote resources to removing the problem.