change background color of the word which is found using a function.
i have a 开发者_JS百科simple swing gui where there are two text fields one for search and another for replace just similar to any other Find and Replace utility.There is a textarea,where the words are found and replaced.This code is working as it should now what I want is, I want to change the background-color or font color just to indicate which word is found (like any other search utilities).How can I do this using java?
I want to change the background-color or font color just to indicate which word is found
You can use a Highlighter:
Highlighter.HighlightPainter cyanPainter
= new DefaultHighlighter.DefaultHighlightPainter( Color.cyan );
...
textPane.getHighlighter().addHighlight( ?, ?, cyanPainter );
Use an EditorKit and JTextPane. Then, as your user inputs his choices for find/replace, use String.replace() to insert color tags into the text of your box to show him what would change. The downside is you will need to remove the colors tags after the editing is completed.
http://download.oracle.com/javase/6/docs/api/javax/swing/JTextPane.html
http://download.oracle.com/javase/6/docs/api/javax/swing/text/rtf/RTFEditorKit.html http://download.oracle.com/javase/6/docs/api/javax/swing/text/html/HTMLEditorKit.html
EDIT: fixed link to HTMLEditorKit
精彩评论