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 5 of 5

Thread: Multiple colors for a textfield

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple colors for a textfield

    Hello,

    I have a question about JTextArea colors. When i set the color to blue and then write something on the JTextArea (JTextArea.append) and then set it back to black everything will be black. How can i solve that? Like in Notepad++ or Eclipse when you write keywords in a JTextArea (where you write your code) only some words change color.

    This is my code:
    // All the imports
     
    public class whatever extends JFrame {
     
    JTextArea a = new JTextArea();
     
    public whatever() {
     
    super("Title");
     
    add(a);
     
    a.setForeground(Color.RED);
    a.append("Hello there");
    a.setForeground(Color.BLACK);
     
    }
     
    }
     
    public static void main(String[] args) {
     
    whatever object = new whatever();
    object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    object.setSize(400, 500);
    object.setVisible(true);
     
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Multiple colors for a textfield

    You cant. The JTextField does not support multiple colors at once.
    You can either subclass the JTextField and overwrite the paintComponent method or you use a different component, for example the JTextPane.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multiple colors for a textfield

    Read the API doc for the class you are working with and see what methods use a color. Then experiment with those methods to see any of them will do what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Multiple colors for a textfield

    I said JTextArea not a JTextField

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Multiple colors for a textfield

    Did you read the API doc for that class?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java Colors
    By Ravekitty in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2013, 08:29 PM
  2. Java3D - Colors mix up
    By Java3D_Noob in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 26th, 2012, 02:24 PM
  3. Replies: 1
    Last Post: April 26th, 2012, 10:06 AM
  4. [SOLVED] Random Colors Problem
    By captain in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 23rd, 2012, 10:40 PM
  5. Replies: 6
    Last Post: December 9th, 2011, 05:53 PM