
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
Display Bold Text Inside JTextArea in Java
JTextArea is one of the most common Swing-based components. By default, it does not support text formatting such as bold, italics, or colored text inside a textarea. In this article, we will learn to display bold text inside the JTextArea in Java.
What is a JTextArea?
A JTextArea class can extend JTextComponent and allow a user to enter multiple lines of text inside it. A JTextArea can generate a CaretListener interface, which can listen to caret update events.
Syntax
The following is the syntax for JTextArea initialization:
JTextArea textArea = new JTextArea();
Adding Bold Text Inside the JTextArea
We can set a font to a text inside the JTextArea by using the setFont() method. The following are the steps for displaying bold text inside the JTextArea in Java:
Class Declaration and Imports
The javax.swing.* provides Swing components (JTextArea, JFrame, etc.) while the java.awt.* provides AWT classes (size, layout managers), and the class extends JFrame to create a window.
import java.awt.*; import javax.swing.*; public class JTextAreaTextBoldTest extends JFrame {
Instance Variable & Constructor Setup
The textArea variable is declared using JTextArea. The constructor initializes the JFrame window with the title "JTextAreaTextBold Test" and sets BorderLayout as the layout manager.
private JTextArea textArea; public JTextAreaTextBoldTest() { setTitle("JTextAreaTextBold Test"); setLayout(new BorderLayout());
JTextArea Setup
Creates a new JTextArea, then setLineWrap(true), which enables line wrapping, and setWrapStyleWord(true), so words are not broken in the middle of a line.
textArea = new JTextArea(); textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
Applying Bold Font
Create a new font with the same name and size but bold style, and use this font on the JTextArea by using the setFont() method.
Font boldFont = new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize()); textArea.setFont(boldFont);
Main Method
The main method launches the application by creating an object of JTextAreaTextBoldTest.
public static void main(String[] args) { new JTextAreaTextBoldTest(); }
Example
Below is an example of displaying bold text inside the JTextArea in Java using the setFont() method:
import java.awt.*; import javax.swing.*; public class JTextAreaTextBoldTest extends JFrame { private JTextArea textArea; public JTextAreaTextBoldTest() { setTitle("JTextAreaTextBold Test"); setLayout(new BorderLayout()); textArea= new JTextArea(); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); Font boldFont=new Font(textArea.getFont().getName(), Font.BOLD, textArea.getFont().getSize()); textArea.setFont(boldFont); // bold text add(textArea); setSize(375, 250); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[]args) { new JTextAreaTextBoldTest(); } }