문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top