Go Back   Java Programming Forums > Learning Java > Java Code Snippets and Tutorials


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 22-05-2008, 03:11 PM
Senior Member
 

Join Date: May 2008
Posts: 19
Thanks: 0
Thanked 2 Times in 1 Post
Flash is on a distinguished road

I'm feeling Sneaky
Post 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:



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);

There are several font settings in the Font class including PLAIN, BOLD, ITALIC and 13 different colors in the Color class (listed below).
  1. BLACK
  2. BLUE
  3. CYAN
  4. DARK_GRAY
  5. GRAY
  6. GREEN
  7. LIGHT_GRAY
  8. MAGENTA
  9. ORANGE
  10. PINK
  11. RED
  12. WHITE
  13. YELLOW
Full code example:

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();
 
    }
}



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 08-07-2008, 08:55 AM
Junior Member
 

Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rohit_n is on a distinguished road
Default 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)
Reply With Quote
  #3 (permalink)  
Old 08-07-2008, 11:17 AM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,581
Thanks: 103
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Stressed
Default Re: How to Change JTextArea font, font size and 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 syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
  #4 (permalink)  
Old 08-07-2008, 06:10 PM
Junior Member
 

Join Date: Jul 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
rohit_n is on a distinguished road
Default 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.
Reply With Quote
  #5 (permalink)  
Old 08-07-2008, 06:45 PM
JavaPF's Avatar
mmm.. coffee
 
7 Highscores

Join Date: May 2008
Location: United Kingdom
Posts: 1,581
Thanks: 103
Thanked 93 Times in 86 Posts
JavaPF is someone you want to know!JavaPF is someone you want to know!JavaPF is someone you want to know!

I'm feeling Stressed
Default 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)
__________________
Don't forget to add syntax highlighted code tags around your code: [highlight=Java] code here [/highlight]

Forum Tip: Add to peoples reputation () by clicking the button on their useful posts.
Reply With Quote
The Following User Says Thank You to JavaPF For This Useful Post:
pjmehta (29-07-2009)
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



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


100 most searched terms
Search Cloud
2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java java 2d arraylist java actionlistener java addactionlistener java convert list to map java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming help java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics two dimensional arraylist java writing ipod apps

All times are GMT. The time now is 06:54 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.