10

I know how to change size, style but how can I set colour of text in Label control? Here is my code so far:

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 there is no colour property in FontData class.

alhcr
  • 823
  • 4
  • 12
  • 21

2 Answers2

25

Make sure you don't mix SWT and AWT colors, and if you build a Color object, make sure you dispose 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 built-in system colors:

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

(Do not dispose the system colors.)

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
2
myLabel.setForeground(Color fg).

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

For more information : see this

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

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • Don't mix AWT `Color` with SWT `Color`. You want `new org.eclipse.swt.graphics.Color(getDisplay(), 102, 255, 102))`. And you need to `dispose` the `Color` when you're done with it. – Edward Thomson Oct 12 '11 at 15:22