Styling a SWT label to be italic
Asked Answered
D

4

12

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");
Dishwasher answered 2/12, 2009 at 1:8 Comment(0)
S
17

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();
Seka answered 2/12, 2009 at 10:8 Comment(1)
And just to think, people said Java was too verboseDub
V
19

It would be better to use FontRegistry class from JFaces, like this:

label.setFont(
    JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT)
);
Volution answered 16/8, 2013 at 11:56 Comment(2)
It should be mentioned that this helper class will automatically dispose all requested fonts. So you no longer need to keep references around to dispose them, which is a nice bonus on top of the brevity of this solution.Redintegrate
Just a small correction: while using an empty string achieves the same effect, the "correct" way to get the default italic font would be getItalic(JFaceResources.DEFAULT_FONT)Dub
S
17

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();
Seka answered 2/12, 2009 at 10:8 Comment(1)
And just to think, people said Java was too verboseDub
L
3

A recent article (February 2014 from Jordi Böhme López) suggest another way to get the current font in order to modify it:

it’s like getting the blueprint of the default font, making some changes and building a new font with the modified blueprint:

Label label = new Label(parent, SWT.NONE);
FontDescriptor descriptor = FontDescriptor.createFrom(label.getFont());
// setStyle method returns a new font descriptor for the given style
descriptor = descriptor.setStyle(SWT.BOLD);
label.setFont(descriptor.createFont(label.getDisplay));
label.setText("Bold Label");
Lindblad answered 10/2, 2014 at 11:27 Comment(5)
it seems you have to do something like descriptor = descriptor.setStyle(SWT.BOLD); This is more a create() than a set() methodFluctuation
This solution does not work 1:1 unfortunately. Can't find the fix either.Attu
the setStyle method returns a new font descriptor for the given style, so better descriptor = descriptor.setStyle(SWT.BOLD) e.g.Insouciant
@kewlbfy Interesting. I have edited the answer accordingly. Does it look more accurate?Lindblad
yes, keep in mind that the descriptor.createFont method does call the Font constructor directly, so unlike the expected design of disposables that have to be disposed in case of a constructor call, the font created here also has to be disposed. maybe add it to the fontregistryInsouciant
J
0

The below code should work:

Label lblSample = new Label(shell, SWT.BORDER_SOLID);
lblSample.setFont(new org.eclipse.swt.graphics.Font(null, "Times New Roman", 12, SWT.BOLD | SWT.ITALIC));
lblSample.setText("Enter Text Here");
Jotun answered 14/9, 2015 at 12:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.