JTextArea color

Tris Rabar

Ranch Hand

Posts: 72

posted 15 years ago

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

Posts: 7023

posted 15 years ago

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.

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!

- Trish -

Dirk Schreckmann

Sheriff

Posts: 7023

posted 15 years ago

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.

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

posted 15 years ago

Thanks Dirk...I myself have looked through these...got some tips and ideas. Thanks again!