diff --git a/core-java-modules/core-java-swing/pom.xml b/core-java-modules/core-java-swing/pom.xml new file mode 100644 index 0000000000..28228ebb29 --- /dev/null +++ b/core-java-modules/core-java-swing/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + core-java-string-swing + core-java-string-swing + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + 20 + 20 + UTF-8 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-swing/src/main/java/com/baeldung/customfont/CustomFonts.java b/core-java-modules/core-java-swing/src/main/java/com/baeldung/customfont/CustomFonts.java new file mode 100644 index 0000000000..28e0da84ae --- /dev/null +++ b/core-java-modules/core-java-swing/src/main/java/com/baeldung/customfont/CustomFonts.java @@ -0,0 +1,55 @@ +package com.baeldung.customfont; + +import javax.swing.*; +import java.awt.*; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class CustomFonts { + public static void main(String[] args) { + usingCustomFonts(); + } + + public static void usingCustomFonts() { + final GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); + final List AVAILABLE_FONT_FAMILY_NAMES = Arrays.asList(GE.getAvailableFontFamilyNames()); + try { + final List 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()); + } + + + JFrame frame = new JFrame("Custom Font Example"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + frame.setLayout(new FlowLayout()); + + JLabel label1 = new JLabel("TEXT1"); + label1.setFont(new Font("Roboto Medium", Font.PLAIN, 17)); + + JLabel label2 = new JLabel("TEXT2"); + label2.setFont(new Font("JetBrainsMono-Thin", Font.PLAIN, 17)); + + frame.add(label1); + frame.add(label2); + + frame.pack(); + frame.setVisible(true); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-swing/target/classes/org/example/Main.class b/core-java-modules/core-java-swing/target/classes/org/example/Main.class new file mode 100644 index 0000000000..5914458bc4 Binary files /dev/null and b/core-java-modules/core-java-swing/target/classes/org/example/Main.class differ diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 27d84c742d..9343343b6e 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -183,6 +183,7 @@ core-java-string-algorithms core-java-string-algorithms-2 core-java-string-apis + core-java-swing core-java-string-apis-2 core-java-string-conversions core-java-string-conversions-2