How to display a bold text inside the JTextArea in Java?


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. We can set a font to a text inside the JTextArea by using setFont() method.

Example

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();
   }
}

Output

Updated on: 10-Feb-2020

970 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements