|
||
|
|||
|
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: Java Code
Font font = new Font("Verdana", Font.BOLD, 12);
txt.setFont(font);
txt.setForeground(Color.BLUE);
Java Code
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();
}
}
|
|
|||
|
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) |
|
||||
|
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)
__________________
Don't forget to add code tags around your code: ![]() Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
|
|||
|
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. |
|
||||
|
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)
__________________
Don't forget to add code tags around your code: ![]() Forum Tip: Add to peoples reputation ( ) by clicking the button on their useful posts.
|
| The Following User Says Thank You to JavaPF For This Useful Post: | ||
pjmehta (29-07-2009) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to set the Listbox size. | jacinto | AWT / Java Swing | 4 | 22-06-2009 12:39 PM |
| How to Get the size of a file in bytes | JavaPF | Java Code Snippets and Tutorials | 1 | 08-06-2009 03:19 PM |
| How to Add scroll bars to JTextArea using JScrollPane | Flash | Java Code Snippets and Tutorials | 1 | 21-05-2009 08:41 AM |
| [SOLVED] simple change to a boolean value in a method call help... | rptech | Object Oriented Programming | 3 | 20-05-2009 10:48 AM |