Setting Colors in SWT

JavaSwtColors

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

To create a color, try this:

Device device = Display.getCurrent ();
Color red = new Color (device, 255, 0, 0);

Solution 3 - Java

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][1]

[1]: http://www.eclipse.org/articles/swt-design-2/swt-design-2.html "Managing Operating System Resources in SWT"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrian GianforcaroView Question on Stackoverflow
Solution 1 - JavaqualidafialView Answer on Stackoverflow
Solution 2 - JavajodonnellView Answer on Stackoverflow
Solution 3 - JavaqualidafialView Answer on Stackoverflow