|
-
November 27th, 2013, 12:34 PM
#1
highlighning a pattern in TextArea, please help
I am doing a very quick program and Basically it is a word finder.
Or a pattern finder using Regular Expressions using both the pattern class and matcher class.
By clicking a "refresh" button the pattern entered by the user in a JTextField will be compile with the current text in the JTextArea.
Using my matcher I will find all indexes of every match there is in the text and highlighted (any color), but it would be cool if I could highlight all matches with random colors. At least get the highlighter to work.
So the problem is the text is not appearing to be highlighted (patterns). I don't even know If I am highlighting all patterns found in my JTextArea.
Please help urgently.
Here is the code I implemented quickly after the user pushes the "refresh button" (which takes you to this method):
NOTE: "area" is my JTextArea where I have the user's current text, where the patterns are supposed to be.
method call:
//if refresh button is pushed
textArea.setText(refreshBox(textArea));
method:
public static String refreshBox(JTextArea area) throws BadLocationException {
// TODO Auto-generated method stub
matcherR = pattern.matcher(area.getText()); //to find pattern
redPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.RED);
while(matcherR.find()){
String str = matcherR.group().toString(); //the matcher found
try {
area.getHighlighter().addHighlight(area.getText().indexOf(str), str.length(), redPainter);
} catch (BadLocationException ex) { ex.printStackTrace(); }
}
return area.getText();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|