How to highlight a word in the JTextArea java using different colors?

45 Views Asked by At

I have a java code which requires highlighting different words in a text area using different colors. I managed to get the default blue color highlighting by

JTextArea textArea=new JTextArea();
textArea.setText('some text in textArea');
String tx=textArea.getText();
Highlighter h=textArea.getHighlighter();
String string_to_highlight=  "text";
int pos=tx.indexOf(string_to_highlight,0);
h.addHighlight(pos, pos+string_to_highlight.length(), DefaultHighliter.DefaultPainter);

Could anyone please help me to get other colors like Red, Yellow, etc. Thanks.

0

There are 0 best solutions below