Set Font and Color to Text of JTextArea Component in Java GUI
Submitted by donbermoy on Tuesday, May 20, 2014 - 22:35.
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.
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.
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
- import java.awt.*;
- import javax.swing.*;
- public jTextArea() {
- txtArea.setText("Encode more text to see scrollbars");
- this.setContentPane(content);
- this.pack();
- }
- txtArea.setTitle("JTextArea Component");
- txtArea.setVisible(true);
- txtArea.setSize(250,140);
- txtArea.setLocation(300,300);
- }
- }
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:

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
- 253 views