- FontData class is a lightweight class describing system fonts.
- A FontData to a Font is analogous to an ImageDescriptor to an Image.
- A FontData class describes the following properties of a Font:
- name
- height
- style: combination of SWT.NORMAL, SWT.ITALIC, and SWT.BOLD
The Font itself is resource-based, i.e., if you create it, you must dispose of it.
You can use FontRegistry to manage allocation and disposal of the fonts.
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class FontRegistry {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell();
shell.setLayout(new GridLayout(1, false));
FontRegistry fontRegistry = new FontRegistry(display);
fontRegistry.put("button-text", new FontData[] { new FontData("Arial", 9, SWT.BOLD) });
fontRegistry.put("code", new FontData[] { new FontData("Courier New", 10, SWT.NORMAL) });
Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP);
text.setFont(fontRegistry.get("code"));
text.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
text.setText("");
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
text.setLayoutData(gd);
Button executeButton = new Button(shell, SWT.PUSH);
executeButton.setText("Execute");
executeButton.setFont(fontRegistry.get("button-text"));
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
17.108.FontRegistry |
| 17.108.1. | Using the FontRegistry |