How to Change JTextArea font, font size and color
Using this code you can change the font and font color in a JTextArea.
The default font and color looks like this:
http://www.javaprogrammingforums.com...tareafont1.bmp
When we apply this code:
http://www.javaprogrammingforums.com...tareafont2.bmp
We have created a JTextArea called txt. With a few simple lines of code you can change its font, color & size settings:
Code Java:
Font font = new Font("Verdana", Font.BOLD, 12);
txt.setFont(font);
txt.setForeground(Color.BLUE);
There are several font settings in the Font class including PLAIN, BOLD, ITALIC and 13 different colors in the Color class (listed below).
- BLACK
- BLUE
- CYAN
- DARK_GRAY
- GRAY
- GREEN
- LIGHT_GRAY
- MAGENTA
- ORANGE
- PINK
- RED
- WHITE
- YELLOW
Full code example:
Code Java:
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();
}
}
Re: How to Change JTextArea font, font size and color
My query is quite similar
I want a part of text in JTextArea as BOLD and rest in default font...
Like this
XYZXYZXYZ
ABCABCABC
Is it possible to have some text as bold and some other text normal in JTextArea??
Also is it possible to change the color of some text and not of others???
(Just like above, instead of font I want to change color)
How to Change JTextArea font, font size and color in Java?
Hey rohit_n,
Welcome to the Java Programming Forums!
Yes it is possible to have some text in JTextArea Bold and some normal.
You can use HTML formatting to do this.
As far as im aware, Swing components such as JLabel & JTextArea support HTML.
Check this link out:
How to Use HTML in Swing Components (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
Re: How to Change JTextArea font, font size and color
It will change the text in JLabel,JButton etc but not in JTextArea.
I have tried this and is also clear from the example given there.
But I was reading on and I found this can be easily done in JEditorPane and JTextPane.
Is it the only way by which it can be done??
If it is possible to change font in JTextArea plz post a very short code snippet to do it!!
Thanks in advance.
Re: How to Change JTextArea font, font size and color
Ah OK I didn't realise you couldn't do it with JTextArea.
Check this link out. It looks like a solution using JTextComponent
Using Text Components (The Java™ Tutorials > Creating a GUI with JFC/Swing > Using Swing Components)
Re: How to Change JTextArea font, font size and color
Re: How to Change JTextArea font, font size and color
Did you notice that the Eclipse IDE - and I expect others to behave the same as well, indicated that the JTextArea's show() method is deprecated? What is the alternative to that method?
Re: How to Change JTextArea font, font size and color
Quote:
Originally Posted by
elisha.java
Did you notice that the Eclipse IDE - and I expect others to behave the same as well, indicated that the JTextArea's show() method is deprecated? What is the alternative to that method?
For future reference, please don't resurrect an old thread for a question like this. Yes, show is deprecated (since 1.1 in fact). And my recommendation would be to rely on the API rather than an IDE
Component (Java Platform SE 6)