Guest User

Untitled

a guest
Jul 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1.  private int stNajdenihBesed=0;
  2.     private void findAllActionPerformed(java.awt.event.ActionEvent evt) {                                        
  3.  
  4.         stNajdenihBesed=0;
  5.  
  6.         String searchedText = searchTextField.getText();
  7.        
  8.         StyledDocument d = textContent.getStyledDocument();
  9.         Style s = textContent.addStyle("BarvaOzadja", null);
  10.         StyleConstants.setBackground(s, Color.yellow);
  11.  
  12.         //iStart = kjer se zacne iskana beseda;
  13.         int iStart = 0;
  14.         int line = 0;
  15.         //kjer se konca iskana beseda
  16.         int iEnd=0;
  17.  
  18.         while(true){
  19.  
  20.             //poisci index iskane besede - isci od zadnje besede dalje
  21.             iStart = textContent.getText().indexOf(searchedText,iEnd);
  22.             //ce iskane besede ni:
  23.             if(iStart==-1){
  24.                 break;
  25.             }
  26.  
  27.             line = getLine(textContent.getText(),iStart);
  28.  
  29.             if(stNajdenihBesed==0){
  30.                 textContent.setCaretPosition(iStart);
  31.                 textContent.grabFocus();
  32.             }
  33.  
  34.             d.setCharacterAttributes(iStart-line, searchedText.length(),textContent.getStyle("BarvaOzadja"),false);
  35.             iEnd=iStart + searchedText.length();
  36.             stNajdenihBesed++;
  37.  
  38.         }
  39.  
  40.     }                                      
  41.  
  42.  
  43.  
  44.  
  45.     // vrne vrstico v kateri se nahaja string, ki se zacne na indexu iStarOfString
  46.     public static int getLine(String textContent, int iStartOfString){
  47.         //index vrstic se zacne z 0
  48.         int line = 0;
  49.         int iOfLine = 0;
  50.  
  51.  
  52.         iOfLine = textContent.indexOf("\n", iOfLine);
  53.        
  54.         while(iOfLine<iStartOfString){
  55.  
  56.             iOfLine = textContent.indexOf("\n", iOfLine);
  57.  
  58.  
  59.             //ce v iskanem textu ni več nobene nove vrstice, potem zaključi
  60.             if(iOfLine == -1 || iOfLine>=iStartOfString){
  61.                 break;
  62.             }
  63.  
  64.             //naslednjo vrstico išči takoj za to
  65.             iOfLine++;
  66.  
  67.  
  68.             //povečaj število vrstic
  69.             line++;
  70.         }
  71.  
  72.         return line;
  73.     }
Add Comment
Please, Sign In to add comment