Using setFont in Java

Rupam Yadav Jan 30, 2023 Nov 30, 2021
  1. Using setFont() to Set a New Font in JFrame
  2. Using setFont() and getFont().deriveFont() to Set a Style in the Existing Font
  3. Using setFont() and Font.createFont() to Set a Custom Font
Using setFont in Java

In this article, we will learn how we can use the setFont() method inherited from java.awt.Container in the javax.swing.JFrame class. As the name suggests, this function sets the font to the components of JFrame.

Using setFont() to Set a New Font in JFrame

In this example, we set a new font to a JFrame component.

First, we create a JFrame object and two labels of the JLabel type. We initialize the labels with their text.

Now we create a Font object called myFont1, and in the constructor, we pass three arguments, first is the font that we want to set, second is the font style which can be called using Font class, and the last argument is the font size that is an int type value.

We create another Font object, myFont2, and set a pass a different font value to it. We call the setFont() function using the JLabel objects and pass the Font objects to them.

After that, we set the position and size of the components using the setBounds() function and add them to the JFrame using add(). At last, we set the size and visibility of the JFrame.

import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame jFrame = new JFrame("Set Font Example");
        JLabel jLabel1, jLabel2;

        jLabel1 = new JLabel("Label with Serif Font");
        jLabel2 = new JLabel("Label with Arial Font");

        Font myFont1 = new Font("Serif", Font.BOLD, 12);
        jLabel1.setFont(myFont1);

        Font myFont2 = new Font("Arial", Font.BOLD, 12);
        jLabel2.setFont(myFont2);

        jLabel1.setBounds(80, 100, 120, 30);
        jLabel2.setBounds(80, 80, 120, 30);

        jFrame.add(jLabel1);
        jFrame.add(jLabel2);
        jFrame.setSize(300, 300);

        jFrame.setLayout(null);
        jFrame.setVisible(true);

    }
}

Output:

Java setFont

Using setFont() and getFont().deriveFont() to Set a Style in the Existing Font

In the previous example, we saw how setFont() can be used to set a new font, but we can use this method also to set a new style to the existing font of the JFrame component.

To achieve this, we first get the component’s font using the component.getFont() and call the deriveFont() function that accepts the style that we want to apply. We pass Font.ITALIC to make the font on the JLabel Italic.

import javax.swing.*;
import java.awt.*;

public class Main {

    public static void main(String[] args) {
        JFrame f = new JFrame("Set Font Example");
        JLabel jLabel1;

        jLabel1 = new JLabel("Label with Italic Style");

        jLabel1.setFont(jLabel1.getFont().deriveFont(Font.ITALIC));
        jLabel1.setBounds(80, 100, 120, 30);

        f.add(jLabel1);
        f.setSize(300, 300);

        f.setLayout(null);
        f.setVisible(true);

    }
}

Output:

Java setFont

Using setFont() and Font.createFont() to Set a Custom Font

In this example, we set a custom font, unlike the first program where we already set the font in the class.

To get the custom font, we download it and store it in the root of our project directory. We use the oswald.ttf font file in this example.

We create a JLabel and initialize it to get the font file we call BufferedInputStream and pass an object of FileInputStream that takes the font file’s path as an argument. Now we get an object of InputStream.

To create a new font, we call createFont() from the Font class and pass the font resource type as the first argument and the InputStream as the second argument. We set the custom font to the JLabel component using the deriveFont() method.

After all of that, now we add the component to the JFrame. The output shows the custom font.

package sample;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {

    public static void main(String[] args) {
        JFrame f = new JFrame("Set Font Example");
        JLabel jLabel;

        jLabel = new JLabel("Label with a Custom Font");

        try {
            InputStream inputStream = new BufferedInputStream(
                    new FileInputStream("oswald.ttf"));

            Font font = Font.createFont(Font.TRUETYPE_FONT, inputStream);

            jLabel.setFont(font.deriveFont(Font.BOLD, 12f));

        } catch (FontFormatException | IOException e) {
            e.printStackTrace();
        }


        jLabel.setBounds(80, 100, 120, 30);

        f.add(jLabel);
        f.setSize(300, 300);

        f.setLayout(null);
        f.setVisible(true);

    }
}

Output:

Java setFont

Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Swing

Related Article - Java JLabel