IchHabsDrauf IchHabsDrauf - 6 months ago 25
Java Question

How to set two fonts for JTextArea in case one is not supported

In HTML&CSS it is possible to set multiple fonts as it can happen that a different computers may not support all fonts. So you can do something like:

font-family: consolas, sans-serif, serif;

So if consolas isn't supported, go with sans-serif.
To set the font in JTextArea i used:

textarea.setFont(new Font("Courier New", Font.PLAIN, 14));

is there a way to check if the font is supported on the current computer and if not, switch to another one?

Thank you in advance.

Answer Source

Take a look at:

java.awt.GraphicsEnvironment.getAvailableFontFamilyNames() java.awt.GraphicsEnvironment.getAllFonts()

This methods returns a list of available system fonts, so you can check if your font is supported

If your font is not supported you can use


This method return the default system font

This is an example function to check if your font is supported

public static boolean isMyFontSupported(String fontname) {
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    for (String installedFont : graphicsEnvironment.getAvailableFontFamilyNames()) {
        if (installedFont.equalsIgnoreCase(fontname)) {
            return true;
    return false;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download