Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.95 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to start and stop highlights?
  2. public void checkWord(DocumentEvent e) {
  3.             try {
  4.                 Highlighter redZigZag = mainTxtTypeArea.getHighlighter();
  5.                 int start = Utilities.getWordStart(mainTxtTypeArea, e.getOffset());
  6.                 int end = Utilities.getWordEnd(mainTxtTypeArea, e.getOffset());
  7.                 int length = end - start;
  8.                 String word = mainTxtTypeArea.getDocument().getText(start, length);
  9.  
  10.                 if(dictionary.find(word) == false && Character.isWhitespace(word.charAt(0)) == false){
  11.                     System.out.println("Incorrect: "+word);
  12.                     //I found a zigzag highlighter online, but the default yields the same result
  13.                     redZigZag.addHighlight(start, end, painter);
  14.                 } else {
  15.                     System.out.println("Correct: "+word);
  16.                 }
  17.  
  18.             } catch (BadLocationException ex) {}
  19.         }
  20.        
  21. private void mainTxtTypeAreaKeyReleased(java.awt.event.KeyEvent evt) {
  22.     checkWord();
  23. }
  24.  
  25. private void checkWord() {
  26.     try {
  27.         int start = Utilities.getWordStart(mainTxtTypeArea, mainTxtTypeArea.getCaretPosition());
  28.         int end = Utilities.getWordEnd(mainTxtTypeArea, mainTxtTypeArea.getCaretPosition());
  29.         int length = end - start;
  30.  
  31.         if(length > 0) {
  32.             StyledDocument doc = mainTxtTypeArea.getStyledDocument();
  33.             SimpleAttributeSet as = new SimpleAttributeSet();
  34.  
  35.             if(dictionary.find(doc.getText(start, length)) == false){
  36.                 StyleConstants.setItalic(as, true);
  37.                 StyleConstants.setForeground(as, Color.red);
  38.             } else {
  39.                 StyleConstants.setItalic(as, false);
  40.                 StyleConstants.setForeground(as, Color.BLACK);
  41.             }
  42.             doc.setCharacterAttributes(start, length, as, false);
  43.         }
  44.     } catch (BadLocationException ex) {
  45.         Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  46.     }
  47. }