Java Font from System getSystemFonts()

Here you can find the source of getSystemFonts()

Description

Returns the system font names within a collection.

License

Open Source License

Return

the system fonts names within a Vector object

Declaration

public static Vector<String> getSystemFonts() 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .  ja v a  2 s . c  om
 * DataViewerUtilities.java  2/6/13 1:04 PM
 *
 * Copyright (C) 2012-2013 Nick Ma
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import java.awt.*;

import java.util.Collections;

import java.util.Vector;

public class Main {
    /**
     * Returns the system font names within a collection.
     *
     * @return the system fonts names within a <code>Vector</code> object
     */
    public static Vector<String> getSystemFonts() {
        GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Font[] tempFonts = gEnv.getAllFonts();

        char dot = '.';
        int dotIndex = 0;

        char[] fontNameChars = null;
        String fontName = null;
        Vector<String> fontNames = new Vector<String>();

        for (int i = 0; i < tempFonts.length; i++) {

            fontName = tempFonts[i].getFontName();
            dotIndex = fontName.indexOf(dot);

            if (dotIndex == -1) {
                fontNames.add(fontName);
            } else {
                fontNameChars = fontName.substring(0, dotIndex).toCharArray();
                fontNameChars[0] = Character.toUpperCase(fontNameChars[0]);

                fontName = new String(fontNameChars);

                if (!fontNames.contains(fontName)) {
                    fontNames.add(fontName);
                }

            }

        }

        Collections.sort(fontNames);
        return fontNames;
    }
}

Related

  1. getDefaultFrc()
  2. getDefaultMapXMLCreatorFontName()
  3. getDefaultMonoFontSize()
  4. getDefaultSansSerifFont()
  5. getSystemFontNames()
  6. getSystemTextHints()