65

This is pretty simple, I come from a swing/awt background.

I'm just wondering what the proper way to set the background color for a SWT widget is?

I've been trying:

widget.setBackground( );

Except I have no idea how to create the color Object in SWT?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
Brian Gianforcaro
  • 26,564
  • 11
  • 58
  • 77

3 Answers3

91

For standard colors (including common colors and default colors used by the operating system) Use Display.getSystemColor(int), and pass in the SWT.COLOR_* constant for the color you want.

Display display = Display.getCurrent();
Color blue = display.getSystemColor(SWT.COLOR_BLUE);
Color listBackground = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);

Note that you do not need to dispose these colors because SWT created them.

Baz
  • 36,440
  • 11
  • 68
  • 94
qualidafial
  • 6,674
  • 3
  • 28
  • 38
60

To create a color, try this:

Device device = Display.getCurrent ();
Color red = new Color (device, 255, 0, 0);
jodonnell
  • 49,859
  • 10
  • 62
  • 67
  • 28
    Make sure you don't forget to dispose of this Color! Otherwise you will leak native resources. – Eddie Jan 27 '09 at 04:47
11

Remember that in SWT you must explicitly dispose any resources that you create when you are done with them. This includes widgets, fonts, colors, images, displays, printers, and GCs. If you do not dispose these resources, eventually your application will reach the resource limit of your operating system and the application will cease to run.

See also: SWT: Managing Operating System Resources

qualidafial
  • 6,674
  • 3
  • 28
  • 38
  • 1
    I suggest you use this [SWTResourceManager](https://www.eclipse.org/ecf/org.eclipse.ecf.docs/api/org/eclipse/ecf/ui/SWTResourceManager.html) – Campa Sep 16 '15 at 08:30