| Author |
JTextArea color
|
Tris Rabar
Ranch Hand
Joined: Feb 27, 2002
Posts: 72
|
|
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!
|
- Trish -
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
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.
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Tris Rabar
Ranch Hand
Joined: Feb 27, 2002
Posts: 72
|
|
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
Joined: Dec 10, 2001
Posts: 7023
|
|
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
Joined: Feb 27, 2002
Posts: 72
|
|
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
Joined: Feb 27, 2002
Posts: 72
|
|
|
What I mean is, with the above method, when the text becomes deselected, the color is restored to its original color?
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
|
You know, if you were to just try it quickly, the answer would soon reveal itself - it's yes.
|
 |
Tris Rabar
Ranch Hand
Joined: Feb 27, 2002
Posts: 72
|
|
Thanks Dirk!
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
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
Joined: Feb 27, 2002
Posts: 72
|
|
|
Thanks Dirk...I myself have looked through these...got some tips and ideas. Thanks again!
|
 |
 |
|
|
subject: JTextArea color
|
|
|