Changing org.eclipse.swt.widgets background color in Windows
Asked Answered
H

7

7

Right now I am trying to change the background color of a org.eclipse.swt.widgets.Button with the following code:

    Button sceneButton = new Button(border, SWT.TOGGLE | SWT.FLAT);  
    sceneButton.setBackground(Color.RED);

This works fine when I run the program in Solaris, but does nothing when I run the code in Windows. Is this possible? If not, is there some kind of workaround that would allow me to change the background color (even if the "color" is an image) while still displaying text in the button? Thanks!

Heads answered 22/7, 2010 at 18:27 Comment(0)
C
5

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.

Cockfight answered 22/7, 2010 at 18:34 Comment(3)
I read that before posting and realized it was probably impossible. I was just wondering if anyone has had this problem/has come up with some kind of workaround, even if said workaround is incredibly hacky.Heads
Maybe a Label with an Image of a button colored with the color you want. And an extra image when you 'click' the button.Cockfight
We just opted to place a BackgroundImage onto the button, which colored the area in between the button and the button's border. Alternatively, we could have used the SWT_AWT Bridge and integrated SWT buttons into the GUI, but that would have been overly complicated and would have taken too much time. Thanks!Heads
I
13

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);
    }
});
Introversion answered 26/11, 2012 at 9:15 Comment(1)
I aslo set color for my readonly Combo to white by your way. But when I dont set background as black color, the rectangle comletely cover the Combo(can't see text). How can I set color for Combo without change background color? ThanksAntho
C
5

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.

Cockfight answered 22/7, 2010 at 18:34 Comment(3)
I read that before posting and realized it was probably impossible. I was just wondering if anyone has had this problem/has come up with some kind of workaround, even if said workaround is incredibly hacky.Heads
Maybe a Label with an Image of a button colored with the color you want. And an extra image when you 'click' the button.Cockfight
We just opted to place a BackgroundImage onto the button, which colored the area in between the button and the button's border. Alternatively, we could have used the SWT_AWT Bridge and integrated SWT buttons into the GUI, but that would have been overly complicated and would have taken too much time. Thanks!Heads
C
4

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.

Chromite answered 18/10, 2010 at 22:9 Comment(2)
Unfortunately, this program is running on both Solaris and Windows, and the behaviors need to be as similar as possible.Heads
A frustratingly large amount of Windows color is controlled by the system theme. I wish you luck with your project.Chromite
S
2

You can simulate a button using CLabel. Add a mouselistener to change the background on mouse down and mouse up, and in the mouse up event dispatch a selection listener event so that it behaves the same as a button. For example:

Color bg = ...
Color shadow = ...
CLabel simulatedButton = new CLabel(parent, SWT.PUSH);
simulatedButton.setBackground(bg); 
simulatedButton.addMouseListener(new MouseAdapter() {

  @Override
  public void mouseUp(MouseEvent e) {
    simulatedButton.setBackground(bg);
    notifyListeners(SWT.Selection, new Event());
  }

  @Override
  public void mouseDown(MouseEvent e) {
    simulatedButton.setBackground(shadow);
  }
});

This briefly changes the background of the button while you are pressing the mouse to give the effect of clicking a button. CLabel can also be extended, unlike other SWT widgets, so you can create a subclass if you need to do this often.

Shalna answered 4/4, 2016 at 20:38 Comment(0)
E
1

No You cannot change the Background of a Button is SWT. You can find this information in Eclipse SWT documentation.

Eclipse SWT Documentation Button

public void setBackground(Color color)

Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.

Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.

Epiphysis answered 21/8, 2019 at 14:22 Comment(0)
F
0

Could it be that the last change in SWT for Windows has changed this? I left the RED background in the code and I have changed the SWT library to the latest and now the background color is changing.

Fleshpots answered 10/2, 2020 at 9:35 Comment(0)
F
0
btnNewButton.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

You can change the background color for a button in SWT for windows with the above code. Few prerequisites should be to:

  • Use these two SWT jars :org.eclipse.swt.win32.win32.x86_64_3.109.0.v20181204-1801.jar and org.eclipse.swt_3.109.0.v20181204-1801.jar. (109 version)
  • Import org.eclipse.wb.swt.SWTResourceManager You can also try latest versions of jars but it worked for me with this. Usually, you can go to advanced properties panel in windows builder to set background but it does not reflect color at runtime.With this, that problem should go away.
Finzer answered 30/6, 2020 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.