Thursday, 22 June 2017

Get All Installed Fonts in a PC ~ foundjava

An example on getting the list of all installed fonts in your PC in Font[] and String[]. 
import java.awt.*;
class GetFonts
{
 public static void main(String args[])
 {

 /*

 Deprecated
 -------------------------

 String[] fonts=Toolkit.getDefaultToolkit().getFontList();
  for(String font:fonts)
  {
  System.out.println(font);
  }

 */

 // Replaced with..
 GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();

 String[] fonts=g.getAvailableFontFamilyNames();

  for(String font:fonts)
  {
  System.out.println(font);
  }

 /*

 To get the Font objects instead of just names..
 ---------------------------------------------------------

 Font[] fonts=g.getAllFonts();

  for(Font font:fonts)
  {
  System.out.println(font);
  }

 */

 }
}

java.awt.GraphicsEnvironment class is an abstract class whose object can be created by using the public static method in it, getLocalGraphicsEnvironment(). This object contains methods, and the one that i've used here to get font family names is getAvailableFontFamilyNames() which returns a String[] containing the names of the fonts that are available (installed) in your PC. If you want the Font[] which represents java.awt.Font objects for use to set font for components, then you can use getAllFonts() method. The Toolkit class has also got it's own method getFontList() which returns the names of fonts installed in the form of a String[] however, that is now deprecated.

Your output might look different because it depends on the fonts installed in your PC. Happy coding :)

An example on getting the list of all installed fonts in your PC in Font[] and String[]. 
import java.awt.*;
class GetFonts
{
 public static void main(String args[])
 {

 /*

 Deprecated
 -------------------------

 String[] fonts=Toolkit.getDefaultToolkit().getFontList();
  for(String font:fonts)
  {
  System.out.println(font);
  }

 */

 // Replaced with..
 GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();

 String[] fonts=g.getAvailableFontFamilyNames();

  for(String font:fonts)
  {
  System.out.println(font);
  }

 /*

 To get the Font objects instead of just names..
 ---------------------------------------------------------

 Font[] fonts=g.getAllFonts();

  for(Font font:fonts)
  {
  System.out.println(font);
  }

 */

 }
}

java.awt.GraphicsEnvironment class is an abstract class whose object can be created by using the public static method in it, getLocalGraphicsEnvironment(). This object contains methods, and the one that i've used here to get font family names is getAvailableFontFamilyNames() which returns a String[] containing the names of the fonts that are available (installed) in your PC. If you want the Font[] which represents java.awt.Font objects for use to set font for components, then you can use getAllFonts() method. The Toolkit class has also got it's own method getFontList() which returns the names of fonts installed in the form of a String[] however, that is now deprecated.

Your output might look different because it depends on the fonts installed in your PC. Happy coding :)

No comments:

Post a Comment