Java allows you to use different fonts in your swing dialog elements. The class GraphicsEnvironment will tell you which fonts are available on your computer. The method to use is called getAvailableFontFamilyNames().
As the method name suggests, you will get a list of “font families”. A font family is a set of font faces that belong together, like regular vs. bold vs. italic vs. bold italic.
Here is what you need to do:
- get an instance of GraphicsEnvironment to do the work
- ask GraphicsEnvironment for a list of the available font families by calling getAvailableFontFamilyNames(). The results are of the type String
- so something useful with the result
Allow for user configuration
Please be aware that the list of available fonts depends on what fonts are installed on the local computer.
So if you develop and test a program an your computer and ship it to some client, then most likely the list of available fonts will differ from yours. Make sure that your program works everywhere. This means that you will need some way to allow the user to configure which fonts to use. Do not rely on a font that the end user doesn’t have.
java font list example code
Here is a sample code to list all available fonts:
package de.evermann.java; import java.awt.GraphicsEnvironment; public class ListAllAvailableFonts { public static void main(String[] args) { String availableFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (String fontFamilyName : availableFonts) { System.out.println(fontFamilyName); } } }
More java stuff can be found here.
image credit:
Bild von Willi Heidelbach on Pixabay