Assuming your JTextArea is called display:
public void findText(String findThis)
//Find the specified string, and highlight it
/**@post findThis is highlighted if found in text area
@param String findThis - the String to find*/
{
int currentPos=display.getCaretPosition();
int docLength=getText().length();
setSelectedText(0,0);
if(getText().indexOf(findThis,currentPos+1)>-1) {
display.setCaretPosition(getText().indexOf(findThi s,currentPos+1));
setSelectedText(display.getCaretPosition(), ( display.getCaretPosition() + findThis.length() ) );
}
else {
if(getText().indexOf(findThis)>-1) {
display.setCaretPosition(getText().indexOf(findThi s));
setSelectedText(display.getCaretPosition(), ( display.getCaretPosition() + findThis.length() ) );
}
}
}
(more context on this method at:
http://www.charlieharvey.com/java/source/NotePad.java )
HTH
charlie
--
Don't Stand on your head - you'll get footprints in your hair.
http://charlieharvey.com