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: Rainbow colored text in Swing

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    722
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Rainbow colored text in Swing

    Hi there.

    I want to display text in a swing panel, problem is, each word is supposed to have a different color.
    I know that I could use a JLabel and use HTML-input into that label. I would really like not to have to do this though.
    For one, my words might include < and/or > signs. Furthermore some of the words are user input and a mischievous user might enter HTML-code to screw with the interface.
    Another thing is, that this big HTML input is not as easy to maintain then having a simpler API. All I need is some color.

    So, if there is any alternative to this, I would really appreciate if anybody could point me in the right direction.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Rainbow colored text in Swing

    a) Create your own JPanel in which you draw each word, alternating the color before drawing a word b) Create multiple JLabels (aligned in the proper LayoutManager), one for each word, and set the foreground color for each JLabel as appropriate. c) Use a JTextPane and add an attribute specifying the appropriate foreground color

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    722
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Rainbow colored text in Swing

    This sounds great, thank you very much.

    Lets assume I know beforehand how many words its going to be, they are all aligned in a single line and I also know the color for each of these.
    Which of the alternatives you have listed would you suggest is the best for the task? Can you give me any kind of pro-contra-list for these?

    Thanks again.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Rainbow colored text in Swing

    Quote Originally Posted by Cornix View Post
    This sounds great, thank you very much.

    Lets assume I know beforehand how many words its going to be, they are all aligned in a single line and I also know the color for each of these.
    Which of the alternatives you have listed would you suggest is the best for the task? Can you give me any kind of pro-contra-list for these?

    Thanks again.
    Best is relative: your layout, other possible requirements, what you are familiar with and time constraints, etc....all play a part. Choice (a) probably gives the greatest flexibility insofar as customizing the rendering, but if you're unfamiliar with custom painting you may opt to go the other route(s) if you are under time constraints.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    722
    Thanks
    0
    Thanked 132 Times in 115 Posts

    Default Re: Rainbow colored text in Swing

    I need to use it as a TreeCellRenderer.
    I think I will go with the JPanels. Seems rather simple compared to the JTextPane API. And I guess it also has better performance for my simple use-case.

    Thank you again.

Similar Threads

  1. k
    By hoangna_it in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 13th, 2012, 10:30 AM
  2. Creating a rainbow code in Java
    By integer in forum Java Theory & Questions
    Replies: 1
    Last Post: June 11th, 2012, 08:09 AM
  3. Converting a picture made up of 0s and 1s into a colored picture??
    By Kranti1992 in forum Java Theory & Questions
    Replies: 10
    Last Post: November 21st, 2011, 05:25 PM
  4. Large Text file(1GB-10GB) Java Swing
    By fortune2k in forum AWT / Java Swing
    Replies: 4
    Last Post: March 27th, 2011, 01:54 PM
  5. Text in Swing
    By whoismrsaxon in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:22 AM