Using the SWT FontDialog Class

The FontDialog class is used to present the user with a standard user interface for selecting fonts for Text widgets.

How do I do that?

The use of FontDialog is similar to that of the dialogs previously discussed. Example 17-5 demonstrates a common use of FontDialog—permitting the user to select the font for a Text widget.

Example 17-5. Using the FontDialog

import org.eclipse.swt.widgets.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;

public class FontDialogExample {
    
    Display d;
    Shell s;
    FontDialogExample( )    {
         d = new Display( );
         s = new Shell(d);
        s.setSize(400,400);
        s.setImage(new Image(d, "c:\\icons\\JavaCup.ico"));
        s.setText("A FontDialog Example");
        s.setLayout(new FillLayout(SWT.VERTICAL));    
        final Text t = new Text(s, SWT.BORDER | SWT.MULTI);
        final Button b = new Button(s, SWT.PUSH | SWT.BORDER);
        b.setText("Change Font");
        b.addSelectionListener(new SelectionAdapter( ) {
            public void widgetSelected(SelectionEvent e) {
                FontDialog fd = new FontDialog(s, SWT.NONE);
                fd.setText("Select Font");
                fd.setRGB(new RGB(0,0,255));
                FontData defaultFont = new FontData("Courier",10,SWT.BOLD);
                fd.setFontData(defaultFont);     
                FontData newFont = fd.open( );
                if(newFont==null) ...

Get SWT: A Developer's Notebook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.