Configuration:
final GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
final List<String> AVAILABLE_FONT_FAMILY_NAMES = Arrays.asList(GE.getAvailableFontFamilyNames());
try {
final List<File> LIST = Arrays.asList(
new File("font/JetBrainsMono/JetBrainsMono-Thin.ttf"),
new File("font/JetBrainsMono/JetBrainsMono-Light.ttf"),
new File("font/Roboto/Roboto-Light.ttf"),
new File("font/Roboto/Roboto-Regular.ttf"),
new File("font/Roboto/Roboto-Medium.ttf")
);
for (File LIST_ITEM : LIST) {
if (LIST_ITEM.exists()) {
Font FONT = Font.createFont(Font.TRUETYPE_FONT, LIST_ITEM);
if (!AVAILABLE_FONT_FAMILY_NAMES.contains(FONT.getFontName())){
GE.registerFont(FONT);
}
}
}
} catch (FontFormatException | IOException exception) {
JOptionPane.showMessageDialog(null, exception.getMessage());
}
Usage example:
JLabel label1 = new JLabel("TEXT1");
label1.setFont(new Font("Roboto Medium", Font.PLAIN, 13));
JLabel label2 = new JLabel("TEXT2");
label2.setFont(new Font("Roboto", Font.PLAIN, 13));
Note:
Only existing fonts that are not available in the system are registered.