CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > Use Font in Java Swing Application

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

Use Font in Java Swing Application

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 · 2 min read · 0 Comments
Share: in X

In the Java Swing application, we can use different fonts for displaying text in graphical user interface components like labels, buttons, text fields, and so on. Following is an example code snippet that demonstrates how to use the font in the Java Swing application.

Let’s see a font used for a JLabel component in the following example:

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class FontDemo extends JFrame {
	public FontDemo() {
		super("Font Demo");
		JLabel label = new JLabel("Hello, world!");
		Font font = new Font("Serif", Font.BOLD, 24); // create a font object
		label.setFont(font); // set the font for the label
		add(label);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new FontDemo();
	}

}

In the above code, we create a new font object using the Font class constructor. The first parameter is the name of the font family, the second parameter is the style (bold, italic, plain, etc.), and the third parameter is the font size. We then set the font for the label component using the setFont() method.

The output of the above code is:

Font Demo

Use custom font

We can use any font family that is installed on our system, or we can load a custom font file using the Font.createFont() method. Following is an example that loads a custom font file and sets it for a JLabel:

import java.awt.Font;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class CustomFontDemo extends JFrame {
	public CustomFontDemo() {
		super("CustomFont Demo");
		JLabel label = new JLabel("Hello, world!");
		try {
			Font font = Font.createFont(Font.TRUETYPE_FONT,
					new File("C:\\Users\\codersathi\\Downloads\\roboto\\Roboto-BoldItalic.ttf")); // load custom font
			font = font.deriveFont(Font.BOLD, 24); // set font style and size
			label.setFont(font); // set the font for the label
		} catch (Exception e) {
			e.printStackTrace();
		}
		add(label);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String[] args) {
		new CustomFontDemo();
	}

}

In this example, I first load a custom font file using the Font.createFont() method. The first parameter is the font type (TrueType or OpenType), and the second parameter is the font file that I’ve downloaded the font called Roboto (Make sure to UnZip it before using). I then set the font style and size using the deriveFont() method, and finally, we set the font for the label component using the setFont() method.

The output of this font looks like:

Roboto font

Related Posts:

  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Java Swing Tutorial
  • Interface in Java: Mastering Abstraction and…
  • MySQL Commands for Developers
  • Control Statements in Java
  • Extends Keyword in Java: A Deep Dive into Inheritance
Tags:javaswing
Was this article helpful?
← Previous ArticleDisable Spring Security for Specific Profile
Next Article →Display Image in Java Swing Application

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap