Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int stNajdenihBesed=0;
- private void findAllActionPerformed(java.awt.event.ActionEvent evt) {
- stNajdenihBesed=0;
- String searchedText = searchTextField.getText();
- StyledDocument d = textContent.getStyledDocument();
- Style s = textContent.addStyle("BarvaOzadja", null);
- StyleConstants.setBackground(s, Color.yellow);
- //iStart = kjer se zacne iskana beseda;
- int iStart = 0;
- int line = 0;
- //kjer se konca iskana beseda
- int iEnd=0;
- while(true){
- //poisci index iskane besede - isci od zadnje besede dalje
- iStart = textContent.getText().indexOf(searchedText,iEnd);
- //ce iskane besede ni:
- if(iStart==-1){
- break;
- }
- line = getLine(textContent.getText(),iStart);
- if(stNajdenihBesed==0){
- textContent.setCaretPosition(iStart);
- textContent.grabFocus();
- }
- d.setCharacterAttributes(iStart-line, searchedText.length(),textContent.getStyle("BarvaOzadja"),false);
- iEnd=iStart + searchedText.length();
- stNajdenihBesed++;
- }
- }
- // vrne vrstico v kateri se nahaja string, ki se zacne na indexu iStarOfString
- public static int getLine(String textContent, int iStartOfString){
- //index vrstic se zacne z 0
- int line = 0;
- int iOfLine = 0;
- iOfLine = textContent.indexOf("\n", iOfLine);
- while(iOfLine<iStartOfString){
- iOfLine = textContent.indexOf("\n", iOfLine);
- //ce v iskanem textu ni več nobene nove vrstice, potem zaključi
- if(iOfLine == -1 || iOfLine>=iStartOfString){
- break;
- }
- //naslednjo vrstico išči takoj za to
- iOfLine++;
- //povečaj število vrstic
- line++;
- }
- return line;
- }
Add Comment
Please, Sign In to add comment