views:

176

answers:

2

I have a custom ButtonUI class that paints the button. Before drawing the text, the paint method checks whether the button has been set a custom color in order to use that instead of UIDefaults#get("Button.foreground").

if ((b.getForeground() != null)) {
    colText = b.getForeground();
}

Having a look into the java.awt.Component class yields a problem:

public Color getForeground() {
    Color foreground = this.foreground;
    if (foreground != null) {
        return foreground;
    }
    Container parent = this.parent;
    return (parent != null) ? parent.getForeground() : null;
}

So checking the button's getForeground() against null doesn't help that much as it will return the foreground color for the component the button is placed on.

Question is: How do I check whether the button has explicitly been set a custom foreground color?

Putting a PropertyChangedListener on the button might be a solution, but I somehow think there must be a simpler way.

+2  A: 

Hi, in the Component you can find the isBackgroundSet() method.

elou
A: 

Override the method and have it return this.foreground; or add a new method that returns that.

Josh
You can only do that if you are producing the Button. This may not be what he wants.
elou