Changing org.eclipse.swt.widgets background color in Windows

On windows operating systems button.setBackGround doesn't work directly. A small snippet of code can help. Override the paint event of button as shown below:-

-----obj is button name in below snippet------------

obj.addPaintListener(new PaintListener() {
@Override
    public void paintControl(PaintEvent arg0) {
    // TODO Auto-generated method stub
    obj.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    org.eclipse.swt.graphics.Pattern pattern;
    pattern = new org.eclipse.swt.graphics.Pattern(arg0.gc.getDevice(), 0,0,0,100, arg0.gc.getDevice().getSystemColor(SWT.COLOR_GRAY),230, arg0.gc.getDevice().getSystemColor(SWT.COLOR_BLACK),230);
    arg0.gc.setBackgroundPattern(pattern);
    arg0.gc.fillGradientRectangle(0, 0, obj.getBounds().width, obj.getBounds().height, true);
    }
});

You can't. In the documentation of method Control.setBackground(), it is mentioned:

For example, on Windows the background of a Button cannot be changed.


The background of a button in Windows is set from outside of SWT.

Right-click your desktop, click Properties.

Go to the "Appearance" tab.

Click "Advanced".

I believe "3D objects" determines the button background. This is determined by each user's theme.

alt text

One great thing about SWT is it uses the underlying system widgets and themes. A frustrating thing about SWT is it uses the underlying system widgets and themes.