SWT provides the FontDialog class to display the common font selection dialog. FontDialog's open() method returns a FontData object (or null if the user cancels the dialog), which you can use to create a Font.
SWT uses two classes to represent fonts:
import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FontDialog; import org.eclipse.swt.widgets.Shell; public class FontSelectionDialogDisplay { public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); FontDialog dlg = new FontDialog(shell); FontData fontData = dlg.open(); if (fontData != null) { Font font = new Font(shell.getDisplay(), fontData); font.dispose(); } display.dispose(); } }
Caution: Don't dispose a font while your application is still using it.
17.106.FontDialog | ||||
17.106.1. | Choosing a Font | |||
17.106.2. | To use the font selection dialog to change both the font and the color of a label |