Font handling in SWT

Author: Kasper Graversen, 21/06/08
Keywords: SWT, Font, Font list, GUI, Java
Abstract: Guide on how to list fonts and load fonts in Eclipse SWT code.
subscribe to my RSS feed


Bookmark and Share


Font handling in SWT

The Eclipse SWT framework is a great framework. It is fast, tidy and free. Unfortunately the documentation seems to lack a bit. I spent some time fiddling with getting font loading to work properly for my latest project Data Storm. Here my secrets are unleashed! The code shows two things:
  • How to see all fonts available to your SWT program
  • how to load a font such as a *.ttf font!
When you load your font, you specify the file name of the file (e.g. *.ttf or *.fon or whatever). After loading the font, you must specify the font name. You can use getFontList() for this, and use a specific name or null for a full font list.
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class FontList {

public static void main(String[] args) {
	Display display = new Display();
	Shell shell = new Shell(display);
	displayAllLoadedFonts(shell);	
	loadFont(shell, "amigaforeverpro2.ttf", 8);
}

private static Font loadFont(Shell shell, String fontFileName, int fontSize) {
	boolean isFontLoaded = shell.getDisplay().loadFont(fontFileName);
	if( isFontLoaded ) {
		displayAllLoadedFonts(shell);
		// font name found by inspecting the font list..
		return new Font(shell.getDisplay(), "Amiga Forever Pro2", fontSize, SWT.NORMAL);
	}
	return null;
}

private static void displayAllLoadedFonts(Shell shell) {
	// display all scalable fonts in the system
	FontData[] fd = shell.getDisplay().getFontList(null, true);
	for( int i = 0; i < fd.length; i++ ) {
		System.out.println(fd[i].getName());
	}
	// and the non-scalable ones
	fd = shell.getDisplay().getFontList(null, false);
	for( int i = 0; i < fd.length; i++ ) {
		System.out.println(fd[i].getName());
	}
}
}

The code is not particularly complicated, the hard part is just figuring out what to write! There! Served on a silver platter for you :-)



Comments

If you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.


If you like this site consider subscribing to my RSS feed or how about subscribing by Email.


Help spread the word

Share this post on your favorite social bookmarking sites:
If you enjoyed this article, found it thought provoking, educative or otherwise good, please link to this page from your page or social bookmarking page. If you have any texts you think are worth publishing on First Class Thoughts, don't hesitate to send me a mail! Quality always welcome.


Bookmark and Share


The most recent contributions
28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers.
13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise"
20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba".
16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words.
Nothing of interest? Try browsing the entire article archive...