Choosing a Font : FontDialog « SWT « Java Tutorial






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:

  1. Font represents the onscreen font, and
  2. Font objects represent operating system resources, and you must dispose any you create.
  3. FontData represents the data used to construct the onscreen font.
  4. FontData objects contain only data, and aren't operating system resources, so they aren't disposed.
Choosing a Font
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 FontChoosing a Font
17.106.2.To use the font selection dialog to change both the font and the color of a labelTo use the font selection dialog to change both the font and the color of a label