This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes set color of just one specific word into the text area. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "set color of just one specific word into the text area." Watch "set color of just one specific word into the text area." New topic
Author

set color of just one specific word into the text area.

rohit rusty
Greenhorn

Joined: Jun 27, 2011
Posts: 15
Hello friends,

i am trying to develop an application and i need to change the color of a particular word in the same TextArea from which it is being read. i have gotten this far :

for(int i = 0;i < inWord.length; i++){ //inWord is a Strin[] having the words that the user has written into the TextArea i.e. txtInput
if((lookup = (String) list.get(inWord[i])) == null){ //to check if the word is present or not, if not present
txtInput.setForeground(Color.red); // change the foreground color
txtInput.replaceRange(inWord[i],startPos,startPos+inWord[i].length()); //and replace the word at the correct position in the TextArea i.e. txtInput with changed color
break;
}
else
startPos = startPos + inWord[i].length()+1; // else find the position of where to replace.
}



the problem, that you all might have guessed by now, is once i use the setForeground() method.... the foreground color of entire TextArea changes to red. whereas i just want to change the color of the word that i am inserting.
Also if there are any methods to directly change the color a particular word in the TextArea without reading it and reinserting it.
please help.

rohitrusty314.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2153
    
    7
You should use a JTextPane.

See Text Component Features for a working example.
 
jQuery in Action, 2nd edition
 
subject: set color of just one specific word into the text area.
 
Similar Threads
Change background/foreground color of a disablled object
select a text from JTextArea and chaange its color
Word wrapping in Canvas !!!!
StyleAttributes not applying when called in quick succession
Need a help on developing multicolored textarea for chat application