Java SWT: how to change text color in Label control?

I know how to change the size, style, but how to set the text color in Label control? Here is my code:

Label myLabel = new Label(shell, SWT.NONE); myLabel.setText("some text that needs to be for example green"); FontData[] fD = myLabel.getFont().getFontData(); fD[0].setHeight(16); fD[0].setStyle(SWT.BOLD); myLabel.setFont( new Font(display,fD[0])); 

I see that the FontData class does not have a color property.

+7
source share
2 answers

Make sure that you do not mix the colors of SWT and AWT, and if you create a Color object, make sure you destroy it. You want something like:

 final Color myColor = new Color(getDisplay(), 102, 255, 102); myLabel.setForeground(color); myLabel.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent e) { myColor.dispose(); } }); 

Or you can just use the system's built-in colors:

 myLabel.setForeground(getDisplay().getSystemColor(SWT.COLOR_GREEN)); 

(Do not use system colors.)

+21
source
 myLabel.setForeground(Color fg). 

color: the Color class is used to encapsulate colors in the default sRGB color space or in colors in arbitrary color spaces denoted by ColorSpace.

For more information: see this.

For green, it will be something like: myLabel.setForeground(new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102));

+2
source

All Articles