Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explicitly set the background color of a SWT Text widget to the default color?

I have a SWT Text field in my UI. If the text field contains an unusual value (as determined by my specific use case) I will set the background color red, to get the users attention. If the value (the text) of the text field changes to a normal value, I would like to set the background color back to default, but I don't see that this is possible.

Is it possible? Can I explicitly change the background color of a SWT Text widget back to default?

like image 686
Buttons840 Avatar asked Dec 28 '22 06:12

Buttons840


2 Answers

Try passing null for the Color argument to setBackground:

text.setBackground(null);
like image 88
Mike Daniels Avatar answered Dec 31 '22 11:12

Mike Daniels


I don't think there is a built-in method that gets the "original color". What I would suggest is keeping a field with the old value, and return it when needed:

class FlashingText extends Text{
    //Enter needed ctors
    private Color originalColor;
    public void markForUser(){
        originalColor = getBackground();
        setBackground(Color.RED);
    }
    public void resetColor(){
        setBackground(originalColor);
    }
}

Notice this will work even if in the future you decide to change the Text's color for design purposes. Also notice giving an entity in your program an object is usually good design.

like image 41
Gilthans Avatar answered Dec 31 '22 12:12

Gilthans