Styling a SWT label to be italic

Simon Lieschke picture Simon Lieschke · Dec 2, 2009 · Viewed 16k times · Source

How would I go about styling a SWT label created along the following lines so it is displayed italicised?

Label label = formToolkit.createLabel(composite, "My label name");

Answer

McDowell picture McDowell · Dec 2, 2009

Create a new Font object.

Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Label label = new Label(shell, SWT.NONE);
label.setText("I am italic");
FontData fontData = label.getFont().getFontData()[0];
Font font = new Font(display, new FontData(fontData.getName(), fontData
    .getHeight(), SWT.ITALIC));
label.setFont(font);
shell.open();
while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}
font.dispose();
display.dispose();