This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextArea color

 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Dirk!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Dirk...I myself have looked through these...got some tips and ideas. Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic