Using this code you can change the font and font color in a JTextArea.
The default font and color looks like this:
When we apply this code:
We have created a JTextArea called txt. With a few simple lines of code you can change its font, color & size settings:
There are several font settings in the Font class including PLAIN, BOLD, ITALIC and 13 different colors in the Color class (listed below).
Full code example:
- BLACK
- BLUE
- CYAN
- DARK_GRAY
- GRAY
- GREEN
- LIGHT_GRAY
- MAGENTA
- ORANGE
- PINK
- RED
- WHITE
- YELLOW
import java.awt.Color; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JTextArea; public class JTextAreaFontandColor extends JFrame { JTextArea txt = new JTextArea(); public JTextAreaFontandColor() { setLayout(null); txt.setBounds(3, 3, 300, 200); add(txt); Font font = new Font("Verdana", Font.BOLD, 12); txt.setFont(font); txt.setForeground(Color.BLUE); txt.setText("\n \n JTextArea font & color change example"); } public static void main(String[] args) { JTextAreaFontandColor jtxt = new JTextAreaFontandColor(); jtxt.setSize(313,233); jtxt.setTitle("JTextArea font & color settings"); jtxt.show(); } }