Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to Change JTextArea font, font size and color

  1. #1
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 9 Times in 4 Posts

    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:

    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:

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

  2. The Following User Says Thank You to Flash For This Useful Post:

    Melawe (May 10th, 2012)


  3. #2
    Junior Member
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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)

  4. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. The Following User Says Thank You to JavaPF For This Useful Post:

    Melawe (May 10th, 2012)

  6. #4
    Junior Member
    Join Date
    Jul 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  7. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    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)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  8. The Following User Says Thank You to JavaPF For This Useful Post:

    pjmehta (July 29th, 2009)

  9. #6
    Junior Member
    Join Date
    Jan 2012
    Location
    Mumbai
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to Change JTextArea font, font size and color

    Good one..

  10. #7
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default 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?

  11. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to Change JTextArea font, font size and color

    Quote Originally Posted by elisha.java View Post
    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)

Similar Threads

  1. How to set the Listbox size.
    By jacinto in forum AWT / Java Swing
    Replies: 4
    Last Post: June 22nd, 2009, 07:39 AM
  2. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM
  3. Replies: 1
    Last Post: May 21st, 2009, 03:41 AM
  4. [SOLVED] Java exception "result already defined"
    By rptech in forum Object Oriented Programming
    Replies: 3
    Last Post: May 20th, 2009, 05:48 AM