5

I'm working with java.awt.Font , How can I discover if the font defined is installed on the system?

Specifically, I have

Font font = new Font("FooBar", 0, 14);

I want to discover if "font" is a valid font on the system and default to something more universal if it isn't available.

Cogman
  • 2,070
  • 19
  • 36

3 Answers3

10

Try checking your Font in a loop like this:

public class SO2 {
  public static void main(String args[]) {
      GraphicsEnvironment g= null;
        g=GraphicsEnvironment.getLocalGraphicsEnvironment();
        String []fonts=g.getAvailableFontFamilyNames();
            for (int i = 0; i < fonts.length; i++) {
            System.out.println(fonts[i]);
            if(fonts[i].equals("YOUR FONT")){
                System.out.println("Found!");
            }
    }

}}

It should output "Found!" if it locates your font.

Good Luck!

Levenal
  • 3,796
  • 3
  • 24
  • 29
2

check it against:

fonts = java.awt.GraphicsEnvironment.getAvailableFontFamilyNames();

and see what comes out and if it's in the list.

also see:

How to find, with Java, if a certain font is installed correctly on a machine

and feel free to read up at:

http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html

the trick with java is to always refer to the documentation.

it's hard at first to read, but once you get it down, it's indispensable.

Community
  • 1
  • 1
Plasmarob
  • 1,321
  • 12
  • 20
2

Use java.awt.GraphicsEnvironment.getAvailableFontFamilyNames() or java.awt.GraphicsEnvironment.getAllFonts() to get java.awt.Font objects

AlpenDitrix
  • 322
  • 2
  • 8