17

Is there any way to get the system default font name in Java? The default font can differ from os. So it can create trouble if we use font Arial and the jar is running in Linux without having Arial font installed.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

8 Answers8

36

Try this:

private final Font FONT = new JLabel().getFont();
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
Salvation
  • 361
  • 1
  • 3
  • 2
11

JavaFX makes this a lot easier:

import javafx.scene.text.Font;

then use:

Font defaultFont = Font.getDefault();

or

// Where 14 is the font size
Font defaultFont = new Font(14);
Troyseph
  • 4,960
  • 3
  • 38
  • 61
Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    Is there a way to do this in Swing? – Cardinal System Dec 02 '17 at 19:51
  • 1
    @CardinalSystem I'd recommend using the name, size, and weight of this JavaFX `Font` to create a Swing `Font`. If you don't like the taste of that, then I think Salvation's answer is for you: https://stackoverflow.com/a/11592041/453435 – Ky - Dec 04 '17 at 13:16
  • 4
    `.getDefault()` returns `System` for me, so this is not that all helpful for me. Any tips to get the /real/ font family? – mazunki May 04 '20 at 16:11
  • @mazunki it's been 7 years since I've had to deal with this. I'd bet [Salvation's answer](https://stackoverflow.com/a/11592041/453435) will work for you if you still need a solution that gets you the _name_ of the font. Otherwise, JavaFX will properly use `"System"` as expected, to render the system's default font. – Ky - Jan 19 '22 at 22:20
  • @Lorenzo in the documentation you linked: "`Font(double size)` Constructs a font using the default face `"System"`." That's just how JavaFX works, I suppose. It's strange, but it behaves as expected at runtime if you're not trying to read the font face name – Ky - Jan 19 '22 at 22:21
7

Use the defined Font constants such as SERIF/SANS_SERIF etc.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
5

I am currently using this to get the default font, although I would rather not need to use a graphics object to get it:

        private final Font getFont()
            {
                Graphics g = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB).getGraphics();
                Font font = new Font(g.getFont().toString(), 0, 12);
                g.dispose();

                return font;
            }
Troyseph
  • 4,960
  • 3
  • 38
  • 61
4

Take a look at public static Font decode(String str) here. When the decode method receives a null pointer as a parameter it returns the "Dialog" font which is usually the system font.

4

I don't think there is a way of retrieving a system default font(in Swing/AWT the font is normally associated with the current LAF and component, for instance), but if your concern is font compatibility - you could check the font you are using against all the system fonts:

GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] allFonts = e.getAllFonts();

and make a "fail-over" choice if it doesn't exist.

ZenMaster
  • 12,363
  • 5
  • 36
  • 59
  • +1 See also [Getting fonts, sizes, bold,…etc](http://stackoverflow.com/questions/6965038/getting-fonts-sizes-bold-etc/6965149#6965149). – Andrew Thompson Aug 21 '11 at 19:25
  • 1
    On the subject of fail-overs, `Font.canDisplayUpTo` is very useful for testing whether specific characters will work - if for example, you are using a CJK language font and need a fallback option that supports those languages. – rococo Dec 04 '17 at 07:49
3

getFont() returns the current font, which is (usually?) the default. I did this to increase font size.

public MyTextArea(){
    Font currentFont = super.getFont();
    String fontName = currentFont.getFontName();
    int fontStyle = currentFont.getStyle();
    int fontSize = currentFont.getSize() + 4;
    super.setFont(new Font(fontName, fontStyle, fontSize));
}
apelade
  • 31
  • 1
  • 1
    or: `Font currentFont = super.getFont(); if(currentFont != null) super.setFont(currentFont.deriveFont(currentFont.getSize2D()+4));` – Ky - Apr 16 '15 at 09:07
-1

In Windows, Segoe UI

Visit http://www.apaddedcell.com/sites/www.apaddedcell.com/files/fonts-article/final/index.html to see a list of preinstalled fonts.

I chose Verdana

ToñitoG
  • 119
  • 1
  • 6