Thursday, July 03, 2008

Getting a List of Installed System Fonts in Java

Previously, you could get a list of fonts installed in the running system by using using the Java AWT Toolkit:
Toolkit tk = Toolkit.getDefaultToolkit();
String[] fontNames = tk.getFontList();
However, the Toolkit.getDefaultToolkit() method has been deprecated in favor of the java.awt.GraphicsEnvironment. The GraphicsEnvironment class describes the collection of java.awt.GraphicsDevice objects and java.awt.Font objects available to a Java application on a particular platform.

To get a list of font names installed on the system, just use the the getLocalGraphicsEnvironment() method:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = ge.getAvailableFontFamilyNames();
There are many other usefull methods in GraphicsEnvironment class, for more information, take a look at the API.

No comments: