Search code examples
javaswingcolorsjtextarea

Changing String Color in Java


I want to change the color of the string I found from user input in JTextArea. I have been searching through internet but couldn't find a solution. I know JTextPane can handle such kind of styling very easily but I am not familiar with JTextPane and its functions. for example I don't know how to enable word-wrap and line-wrap which I can easily do in a text area.

So is there a way to change the color of a string in JTtextArea?


Solution

  • Either you use a JEditorPane which can take multiple fonts and formats or you set the Background/Foreground colour of your text area.

    setForeground(Color Object Here);
    setBackground(Color Object Here);