user3437460
user3437460

Reputation: 17454

How to get list of font names from Java

In Java we can create a Font object as such:

new Font("Helvetica", Font.PLAIN, 12);

My question is how do we get the entire list of font names from Java, for example "Helvetica" which we can use it as one the argument for the Font constructor?

I tried the following, but I can't find "Helvetica" in all of the lists.

    GraphicsEnvironment ge;  
    ge = GraphicsEnvironment.getLocalGraphicsEnvironment();  

    String[] names = ge.getAvailableFontFamilyNames();
    Font[] allFonts = ge.getAllFonts();

    for(int x=0; x<names.length; x++)
        System.out.println(names[x]);

    for(int x=0; x<allFonts.length; x++){           
        System.out.println(allFonts[x].getName());
        System.out.println(allFonts[x].getFontName());
        System.out.println(allFonts[x].getFamily());
        System.out.println(allFonts[x].getPSName());
    }

Edit: More importantly, I also want to know what is the first attribute call in Font constructornew Font("What attribute is this?", Font.PLAIN, 12)

Q: Is it a fontName, family, fontFace, name or what?

Upvotes: 12

Views: 19841

Answers (4)

Walid Bousseta
Walid Bousseta

Reputation: 1469

this programme will show you list of all font in you system :

import java.awt.GraphicsEnvironment;

public class ListJavaFonts
{

  public static void main(String[] args)
  {
    String fonts[] = 
      GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    for ( int i = 0; i < fonts.length; i++ )
    {
      System.out.println(fonts[i]);
    }
  }

}

Upvotes: 4

Tarm
Tarm

Reputation: 193

I know this is an old question, but there are some unanswered questions.

More importantly, I also want to know what is the first attribute call in Font constructor new Font("What attribute is this?", Font.PLAIN, 12) Q: Is it a fontName, family, fontFace, name or what?

If you decompile the Java class with an IDE (I'm using IntelliJ), you will see:

public Font(String name, int style, int size) {
    this.name = (name != null) ? name : "Default";
    this.style = (style & ~0x03) == 0 ? style : 0;
    this.size = size;
    this.pointSize = size;
}
public String getName() {
        return name;
    }
public String getFontName() {
  return getFontName(Locale.getDefault());
}

This tells you that the name you use when calling the constructor can be retrieved with getName, but a call to getFontName will return your default text. That is why you can set the name to Helvetica, then call getFontName and it return something other than Helvetica.

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

new Font("Helvetica", Font.PLAIN, 12);

In this case, it is better to use something like:

new Font(Font.SANS_SERIF, Font.PLAIN, 12);

That will produce the undecorated Font used by default on that OS.

On Windows it would be Arial. On OS X it would be Helvetica. On *nix machines it might be either, or a 3rd undecorated Font.


In answer to your specific question, I've always found the 'font family' string to be useful for creating an instance of the font.

Upvotes: 4

Elliott Frisch
Elliott Frisch

Reputation: 201437

On your system, that font may well be mapped to something else

Font helvetica = new Font("Helvetica", Font.PLAIN, 12);
System.out.println(helvetica.getFontName(Locale.US));

and I get

SansSerif.plain

To output the names of all local fonts, you could use something like

GraphicsEnvironment ge = GraphicsEnvironment
        .getLocalGraphicsEnvironment();

Font[] allFonts = ge.getAllFonts();

for (Font font : allFonts) {
    System.out.println(font.getFontName(Locale.US));
}

Upvotes: 8

Related Questions