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");
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");
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();
It would be better to use FontRegistry
class from JFaces
, like this:
label.setFont(
JFaceResources.getFontRegistry().getItalic(JFaceResources.DEFAULT_FONT)
);
getItalic(JFaceResources.DEFAULT_FONT)
–
Dub 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();
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");
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");
© 2022 - 2024 — McMap. All rights reserved.