Mega Code Archive

 
Categories / Java Tutorial / SWT
 

Choosing a Font

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: Font represents the onscreen font, and Font objects represent operating system resources, and you must dispose any you create. FontData represents the data used to construct the onscreen font. FontData objects contain only data, and aren't operating system resources, so they aren't disposed. 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.