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!
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!
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..
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.