Set Font and Color to Text of JTextArea Component in Java GUI

Language

This is a continuation of my other tutorial entitled JTextArea Component in Java GUI. This tutorial will teach you how to set/select font and color for the JTextArea or JTextField component. 1. First and foremost, Open JCreator or NetBeans and make a java program with a file name of jTextArea.java. When you click the link above, copy first all the code there.
  1. import java.awt.*;
  2. import javax.swing.*;
  3.  
  4. public class jTextArea extends JFrame {
  5.  
  6. JTextArea txtArea = new JTextArea(5,18);
  7.  
  8. public jTextArea() {
  9.  
  10. txtArea.setText("Encode more text to see scrollbars");
  11. JScrollPane scrollingArea = new JScrollPane(txtArea);
  12.  
  13. JPanel content = new JPanel();
  14. content.setLayout(new BorderLayout());
  15. content.add(scrollingArea, BorderLayout.CENTER);
  16.  
  17. this.setContentPane(content);
  18. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. this.pack();
  20. }
  21.  
  22. public static void main(String[] args) {
  23. JFrame txtArea = new jTextArea();
  24. txtArea.setTitle("JTextArea Component");
  25. txtArea.setVisible(true);
  26. txtArea.setSize(250,140);
  27. txtArea.setLocation(300,300);
  28. }
  29. }
2. Now, insert this code after the JScrollPane scrollingArea = new JScrollPane(txtArea);. This code will add font and color to the text of JTextArea Component.
  1. Font font = new Font("Arial", Font.ITALIC, 12);
  2. txtArea.setFont(font);
  3. txtArea.setForeground(Color.RED);
We have instantiated the Font class to get the font style, size, and type. Here, we used the Font "Arial" that is in the double quote, ITALIC as the font style, and 12 as the size of our font. After instantiating, the txtArea variable for JTextArea component uses the setFont method that has the parameter of the font variable that we have created. This will trigger to set the font for the JTextArea. We also used the setForeground method with a Color.RED in the parameter to make the text in the JTextArea to be in color Red. Make sure that after the Color class, capitalization is required for the color. Output: output Hope this hepls! :) Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment