• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTextArea color

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!
Is there a way to highlight (or color) a part of the text on a textarea? I have text on the area, strings, and am altering some characters in the string. I want to highlight the chars that I'm modifying. How do I do this?
Thanks a lot!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though it may not be exactly what you are hoping to do...
jTextArea.setCaretPosition(1);
jTextArea.moveCaretPosition(2);

or

jTextArea.select(1, 2);

will select consecutive characters only. You can't

jTextArea.select(1, 2);

and

jTextArea.select(3, 4);

and end up with two seperate selections at once. Only the most recent selection is selected (highlighted).
Note: The documentation recommends the set and moveCaretPosition methods as the preferred way to manage the selection.
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Dirk! This would highlight or select the specified characters right? So, if I want to color them, does the following make sense?
setSelectionStart() and setSelectionEnd() to select the text. Then, setSelectedTextColor() to change the color of the text.
This is for anyone else who has tried to achieve this with a JTextArea also!

Thanks again!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the text to change color only when it is selected, then that just might be the way you want to do it.
Good Luck.
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I guess there's no way with using a JTextArea that I can change the color even if the text is not selected? I may have to change over to a JTextPane if I want to achieve this?
Thanks..
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I mean is, with the above method, when the text becomes deselected, the color is restored to its original color?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know, if you were to just try it quickly, the answer would soon reveal itself - it's yes.
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dirk!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a tip...
I did a quick search on this forum for "JTextArea color" in the subject heading (any date), and it produced a couple of promising looking hits.
The search feature can be found at the top right of this page.
Good Luck.
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dirk...I myself have looked through these...got some tips and ideas. Thanks again!
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic