
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement JLabel Text with Different Color and Font in Java
In this article, we will learn to implement a JLabel text with different colors and fonts in Java. JLabel is commonly used for simple text display, it can be enhanced to show text with multiple colors and fonts.
JLabel
A JLabel class can extend the JComponent class, and an object of JLabel provides text instructions or information on a GUI. A JLabel can display a single line of read-only text, an image, or both text and an image. A JLabel can explicitly generate a PropertyChangeListener interface.
Different Approaches
The following are the two different approaches to implementing a JLabel text with a different color and font in Java:
Using the Font Tag
A JLabel can also display a single line of text with different colors and fonts using <font size='font-size' color='color of the text'> Some Text </font> tag inside an HTML tag.
Syntax
The following is the syntax for Font initialization:
<font attribute="value">text</font>
Font Tag Attributes:
- Size Attribute: The size attribute ranges from 1 to 7, with 3 being the default size.
- Color Attribute: Colors can be specified using color names, hexadecimal values, and RGB values.
- Face Attribute: Specifies the font family to be used.
Example
Below is an example of implementing a JLabel text with a different color and font using the Font tag:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MultiColorLabelTest extends JFrame { public MultiColorLabelTest() { setTitle("MultiColorLabel Test"); setLayout(new FlowLayout()); // multi colored with different font size label JLabel label = new JLabel("<html><font size='5' color=blue> Welcome to</font> <font size='6'color=green> Tutorials Point</font></html>"); add(label); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MultiColorLabelTest(); } }
Output
Using setFont() method
In font customization with the setFont() method, we can change fonts with different properties, and with the setForeground() method, we can introduce different colors to the font.
Basic Font attributes:
Sets font family, style, and size of the text.
Text with Times New Roman font, as plain text with text size 16:
JLabel label = new JLabel("Custom Font"); label.setFont(new Font("Times New Roman", Font.PLAIN, 16));
Font Styles:
Available font styles include the Font.PLAIN, Font.BOLD, Font.ITALIC and Font.BOLD + Font.ITALIC.
Bold and italic combination with Arial Font:
JLabel label = new JLabel("Styled Text"); label.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 18));
Customizing Text Color
There are several methods to set the text color in a JLabel:
Using Predefined Colors
Sets text color to a predefined Java color like RED, BLUE, GREEN, etc.
JLabel label = new JLabel("Red Text"); label.setForeground(Color.RED);
Using RGB Values
Sets color using the RGB range's Red, Green, and Blue values (0-255).
JLabel label = new JLabel("Custom Color"); label.setForeground(new Color(128, 0, 128)); // Purple
Using Hexadecimal Values
Sets color from a hex string like #RRGGBB.
JLabel label = new JLabel("Hex Color"); label.setForeground(Color.decode("#FF4500")); // Orange Red
Example
Below is an example of implementing a JLabel text with different color and font using setFont() and setForeground():
import java.awt.*; import javax.swing.*; public class MultiColorLabelTest extends JFrame { public MultiColorLabelTest() { setTitle("MultiColorLabel Test"); setLayout(new FlowLayout()); JPanel textPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); JLabel part1 = new JLabel("Welcome to "); part1.setFont(new Font("Arial", Font.PLAIN, 20)); part1.setForeground(Color.BLUE); textPanel.add(part1); JLabel part2 = new JLabel("Tutorials Point"); part2.setFont(new Font("Arial", Font.PLAIN, 24)); part2.setForeground(Color.GREEN); textPanel.add(part2); add(textPanel); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new MultiColorLabelTest(); } }