Displaying A List Of All Available Fonts Using Java
Kushal Paudyal August 24th, 2010
Most of the tutorials I write in my blogs are because one way or the other I need these programs for myself. I recently ran into a similar situation. I was working on one of application where I was trying to resize an image and put a water mark on top of this image. (This will be my upcoming tutorial). Then I needed a good looking font. I knew there are lots of Fonts installed on my machine, and I can get a preview of them when I use Adobe Photoshop. But I had no idea what would their names be to be used in my application. So I did a little research and wrote this program that list all available fonts from the local system.
First I found the method from ToolKit class. I wrote a simple class to use that method. To my wonder, it just printed 5 fonts. Definitely I didn’t have just five fonts in my machine. The method I used Toolkit.getDefaultToolkit().getFontList(), which I later discovered was a depricated method and only provided me a few primitive fonts.
So, another small research led me to GraphicsEnvironment.getAvailableFontFamilyNames() method which was what I exactly wanted. It listed the names of all the fonts that are available in my machine.
I am presenting you here a class that uses both the methods mentioned above to find a list of available fonts.
/**
* @author Kushal Paudyal
* www.sanjaal.com/java
* Last Modified On: 05-21-2009
*/
package com.kushal.utils;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
/**
* Demonstrating How To Get the
* Available Font Names Using Java
*
*/
public class AvailableFontLister {
public static void main(String args [])
{
System.out.println("***************************");
System.out.println("**Listing Fonts Using Deprecated Method From ToolKit Class**");
listUsingToolKit();
System.out.println();
System.out.println("****************************");
System.out.println("**Listing Fonts Using Method From GraphicsEnvironment Class**");
listUsingGraphicsEnvironment ();
}
/**
* Prints a list of all available fonts from
* the local graphics environment.
*
* The output list varies from manchine to machine
*/
public static void listUsingGraphicsEnvironment ()
{
GraphicsEnvironment ge= null;
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontNames=ge.getAvailableFontFamilyNames();
for (int i = 0; i< fontnames.length; i++) {
System.out.println(fontnames[i]);
}
}
}
———————————–
Sample Output Of This Program On My Machine
*************************** **Listing Fonts Using Deprecated Method From ToolKit Class** Dialog SansSerif Serif Monospaced DialogInput <!--more--> **************************** **Listing Fonts Using Method From GraphicsEnvironment Class** Agency FB Algerian Arial Arial Black Arial Narrow Arial Rounded MT Bold Arial Unicode MS Baskerville Old Face Bauhaus 93 Bell MT Berlin Sans FB Berlin Sans FB Demi Bernard MT Condensed Blackadder ITC Bodoni MT Bodoni MT Black Bodoni MT Condensed Bodoni MT Poster Compressed Book Antiqua Bookman Old Style Bookshelf Symbol 7 Bradley Hand ITC Britannic Bold Broadway Brush Script MT Calibri Californian FB Calisto MT Cambria Cambria Math Candara Castellar Centaur Century Century Gothic Century Schoolbook Chiller Colonna MT Comic Sans MS Consolas Constantia Cooper Black Copperplate Gothic Bold Copperplate Gothic Light Corbel Courier New CourierThai Curlz MT Devanagari MT for IBM Dialog DialogInput Edwardian Script ITC Elephant Engravers MT Eras Bold ITC Eras Demi ITC Eras Light ITC Eras Medium ITC Estrangelo Edessa Felix Titling Footlight MT Light Forte Franklin Gothic Book Franklin Gothic Demi Franklin Gothic Demi Cond Franklin Gothic Heavy Franklin Gothic Medium Franklin Gothic Medium Cond Freestyle Script French Script MT Garamond Gautami Georgia Gigi Gill Sans MT Gill Sans MT Condensed Gill Sans MT Ext Condensed Bold Gill Sans Ultra Bold Gill Sans Ultra Bold Condensed Gloucester MT Extra Condensed Goudy Old Style Goudy Stout Haettenschweiler Harlow Solid Italic Harrington High Tower Text Impact Imprint MT Shadow Informal Roman Jokerman Juice ITC Kristen ITC Kunstler Script Latha Lucida Bright Lucida Calligraphy Lucida Console Lucida Fax Lucida Handwriting Lucida Sans Lucida Sans Typewriter Lucida Sans Unicode Magneto Maiandra GD Mangal Marlett Matura MT Script Capitals Microsoft Sans Serif Mistral Modern No. 20 Monospaced Monotype Corsiva MS Mincho MS Outlook MS Reference Sans Serif MS Reference Specialty MT Extra MV Boli Niagara Engraved Niagara Solid OCR A Extended Old English Text MT Onyx Palace Script MT Palatino Linotype Papyrus Parchment Perpetua Perpetua Titling MT Playbill Poor Richard Pristina Raavi Rage Italic Ravie Rockwell Rockwell Condensed Rockwell Extra Bold SansSerif Script MT Bold Segoe UI Serif Showcard Gothic Shruti Snap ITC Stencil Sylfaen Symbol Tahoma Tempus Sans ITC Thonburi Times New Roman Trebuchet MS Tunga Tw Cen MT Tw Cen MT Condensed Tw Cen MT Condensed Extra Bold Verdana Viner Hand ITC Vivaldi Vladimir Script Webdings Wide Latin Wingdings Wingdings 2 Wingdings 3
Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!
Originally posted 2009-05-21 14:52:51.