Monday 15 February 2010

java - How To Search the Word In Textarea -



java - How To Search the Word In Textarea -

i want highlight word caret position end of textarea highlight word starting of textarea end repeat process in circular manner.my code works current caret position end of textarea only.please check once.

my code:

public void highlightwholeword(jtextarea component, string patteren) { seek { document doc = component.getdocument(); string text = component.gettext(0, doc.getlength()); int pos = component.getcaretposition(); boolean found = false; int findlength = patteren.length(); // rest search position if we're @ end of document if (pos + findlength > doc.getlength()) { pos = 0; } while (pos + findlength <= doc.getlength()) { // extract text teh docuemnt string match = doc.gettext(pos, findlength).tolowercase(); // check see if matches or request if (match.equals(patteren)) { if (pos - 1 >= 0 && character.iswhitespace(doc.gettext(pos - 1, 1).charat(0)) || character.iswhitespace(doc.gettext(findlength, 1).charat(0))) { if (pos + findlength == doc.getlength() || character.iswhitespace(doc.gettext(pos + findlength, 1).charat(0))) { found = true; break; } } } pos++; } if (found) { component.setselectionstart(pos); component.setselectionend(pos + patteren.length()); component.getcaret().setselectionvisible(true); } } grab (exception e) { e.printstacktrace(); } }

let's see text example, , suppose searching word "xxx". suppose lastly time clicked button selected lastly xxx:

xxx ttt aaa xxx fff xxx yyy xxx zzz

the size of text 35 characters.

now, caret position @ end of highlighted xxx (position 31). , pattern length 3. have if status says:

if (pos + findlength > doc.getlength()) { pos = 0; }

now, pos + findlength 31+3 34. not greater doc.getlength(). not set pos = 0.

so start searching xxx position 31, there no more xxx there. zzz. you'll reach end of document , found not set. selection not changed, , caret remain in position 31.

so no matter how many times press button, same thing happen.

this means status setting pos=0 wrong. work if xxx absolutely lastly word.

here suggestion solve this:

define status in method:

public boolean foundpattern( document doc, int pos, int findlength ) { string match = doc.gettext(pos, findlength).tolowercase(); // check see if matches or request if (match.equals(patteren)) { if (pos - 1 >= 0 && character.iswhitespace(doc.gettext(pos - 1, 1).charat(0)) || character.iswhitespace(doc.gettext(findlength, 1).charat(0))) { if (pos + findlength == doc.getlength() || character.iswhitespace(doc.gettext(pos + findlength, 1).charat(0))) { homecoming true; } } } homecoming false; }

this create writing actual while part easier:

while (pos + findlength <= doc.getlength()) { found = foundpattern( doc, pos, findlength ); if ( found ) { break; } pos++; } if ( ! found ) { pos = 0; while ( pos + findlength < component.getcaretposition() ) { found = foundpattern( doc, pos, findlength ); if ( found ) { break; } pos++; } }

so have 2 loops. if first doesn't find pattern (from caret position end of document), sec 1 starts looking start of document caret position.

java search jtextarea

No comments:

Post a Comment