How to get the default system font in a Java/Swing application

I haven’t tested this with other Java components, but if you want/need to get the “system font”, this code gets the default system font from a JEditorPane component:

val outputArea = new JEditorPane
val fontFamily = outputArea.getFont.getFamily

That code is written in Scala, but as you can see, it converts easily to Java. On Mac OS X 10.10, fontFamily ends up being “Lucida Grande”.

Of course if you just want to get the Font and not the FontFamily, just shorten that second line to this:

val font = outputArea.getFont

In a small Scala/Java/Swing app I just wrote, I use that font family to set the font of other components, like this JButton:

// this works to set a font, in this case on a button
goButton.setFont(new Font(fontFamily, Font.PLAIN, 15))
goButton.setPreferredSize(new Dimension(82, 36))

In that case I also set the JButton size because I didn’t like the default size.

In summary, if you want to know how to get the default system font in a Java/Swing application, I hope this is helpful, or at least points you in the right direction.