How to display text in a Swing application?

Jan 02, 2026|

Displaying text in a Swing application is a fundamental yet crucial aspect of creating user - friendly and informative graphical user interfaces (GUIs). As a Swing system supplier, we understand the importance of presenting text accurately and aesthetically in Swing - based applications. In this blog post, we'll explore various methods and best practices for displaying text in a Swing application.

JLabel: The Simplest Way to Display Static Text

The JLabel class is the most straightforward way to display static text in a Swing application. A JLabel is a lightweight component that can display a single line of read - only text, an image, or both.

Here is a simple example of using JLabel:

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

public class JLabelExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("JLabel Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);

            JLabel label = new JLabel("This is a simple JLabel.");
            frame.add(label);

            frame.setVisible(true);
        });
    }
}

In this code, we create a JFrame and a JLabel with some text. Then we add the JLabel to the JFrame and make the frame visible. The text in the JLabel is static, meaning it cannot be edited by the user.

5Swing Platform For Excavator

JTextField: For Single - Line Editable Text

If you need the user to enter single - line text, JTextField is the component to use. A JTextField allows the user to input and edit a single line of text.

Here is an example:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class JTextFieldExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("JTextField Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);

            JTextField textField = new JTextField("Enter your text here");
            frame.add(textField);

            frame.setVisible(true);
        });
    }
}

In this example, we create a JFrame and a JTextField with some default text. The user can click on the JTextField and edit the text as needed.

JTextArea: For Multi - Line Text

When you need to display or allow the user to enter multi - line text, JTextArea is the appropriate component. A JTextArea can handle multiple lines of text and can be made editable or read - only.

Here is an example of using JTextArea:

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class JTextAreaExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("JTextArea Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);

            JTextArea textArea = new JTextArea("This is a multi - line text area.\nYou can enter multiple lines of text here.");
            frame.add(textArea);

            frame.setVisible(true);
        });
    }
}

In this code, we create a JFrame and a JTextArea with some initial multi - line text. The user can scroll through the text if it exceeds the visible area of the JTextArea.

Formatting Text

Swing provides several ways to format text. For JLabel, JTextField, and JTextArea, you can set the font, color, and alignment of the text.

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import java.awt.Font;
import java.awt.Color;

public class TextFormattingExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Text Formatting Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);

            JLabel label = new JLabel("Formatted Text");
            Font font = new Font("Arial", Font.BOLD, 20);
            label.setFont(font);
            label.setForeground(Color.RED);
            label.setHorizontalAlignment(JLabel.CENTER);

            frame.add(label);
            frame.setVisible(true);
        });
    }
}

In this example, we set the font of the JLabel to Arial, bold, and size 20. We also set the text color to red and center the text horizontally.

Using HTML in Swing Text Components

Swing components support HTML for more advanced text formatting. You can use HTML tags within the text of JLabel, JTextArea, etc.

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

public class HTMLTextExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("HTML Text Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 200);

            JLabel label = new JLabel("<html><h1>HTML Heading</h1><p>This is a paragraph with <b>bold</b> and <i>italic</i> text.</p></html>");
            frame.add(label);

            frame.setVisible(true);
        });
    }
}

In this code, we use HTML tags to create a heading, a paragraph, and format some text as bold and italic within a JLabel.

Our Swing Systems for Excavators

As a Swing system supplier, we offer a wide range of high - quality Swing systems for excavators. Our products include the Swing System Assembly for Excavator, Slewing Ring Bearings, and Swing Platform for Excavator. These components are designed to provide reliable and efficient performance in excavator applications.

Conclusion and Call to Action

Displaying text in a Swing application can be achieved in multiple ways, from simple static text using JLabel to editable multi - line text with JTextArea. By understanding these components and their formatting options, you can create more user - friendly and visually appealing Swing applications.

If you are interested in our Swing systems for excavators or have any questions about text display in Swing applications, we encourage you to contact us for procurement and further discussions. Our team of experts is ready to assist you in finding the right solutions for your needs.

References

  • "Java Tutorials: Creating a GUI with JFC/Swing", Oracle
  • "Effective Java", Joshua Bloch
Send Inquiry