By Jazzy

2011-08-06 05:42:12 8 Comments

I'm having trouble finding stuff on accessing Windows fonts or predefined fonts, and sizes. So for my java program I have a JComboBox with fonts, sizes, and colors. The problem is that I need to pre-Enter the fonts, sizes and colors. How would I be able to get the predefined fonts, colors, and sizes? So far this is what I have for this font but its not in the correct way.

               if (font.equals("Arial")) {

                if (size.equals("8")) {
                    setSize = 8;
                } else if (size.equals("10")) {
                    setSize = 10;
                } else if (size.equals("12")) {
                    setSize = 12;

                if (color.equals("Black")) {
                    setColor = Color.BLACK;
                } else if (color.equals("Blue")) {
                    setColor = Color.BLUE;
                } else if (color.equals("Red")) {
                    setColor =;

                Font font = new Font("Arial", setAttribute, setSize);


@Andrew Thompson 2011-08-06 06:13:35

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fonts = ge.getAvailableFontFamilyNames();

The sizes and styles can be set at run-time.


Font Chooser

import java.awt.*;
import javax.swing.*;

class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.
                String[] fonts = ge.getAvailableFontFamilyNames();
                JComboBox fontChooser = new JComboBox(fonts);
                fontChooser.setRenderer(new FontCellRenderer());
                JOptionPane.showMessageDialog(null, fontChooser);

class FontCellRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {
        JLabel label = (JLabel)super.getListCellRendererComponent(
        Font font = new Font((String)value, Font.PLAIN, 20);
        return label;


The JDoc for GraphicsEnvironment.getAvailableFontFamilyNames() state in part..

Returns an array containing the names of all font families in this GraphicsEnvironment localized for the default locale, as returned by Locale.getDefault()..

See also:


@Jazzy 2011-08-06 07:03:06

Wow, this was exactly what I was looking for. So GraphicsEnvironment gets the types of fonts that your system is running on?

@Andrew Thompson 2011-08-06 07:24:30

See the edits for JavaDoc quotes/links.

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] CSS Font Border?

  • 2010-04-03 10:35:02
  • Lars Tackmann
  • 714159 View
  • 466 Score
  • 10 Answer
  • Tags:   css fonts

47 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

7 Answered Questions

[SOLVED] How to add multiple font files for the same font?

18 Answered Questions

[SOLVED] How can I change font size in Eclipse for Java text editors?

26 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1059774 View
  • 1888 Score
  • 26 Answer
  • Tags:   java enums

32 Answered Questions

[SOLVED] Can I embed a custom font in an iPhone application?

  • 2008-12-11 20:21:11
  • Airsource Ltd
  • 235849 View
  • 771 Score
  • 32 Answer
  • Tags:   ios cocoa-touch fonts

21 Answered Questions

[SOLVED] How to add some non-standard font to a website?

17 Answered Questions

[SOLVED] How to set the font size in Emacs?

Sponsored Content