- How to start and stop highlights?
- public void checkWord(DocumentEvent e) {
- try {
- Highlighter redZigZag = mainTxtTypeArea.getHighlighter();
- int start = Utilities.getWordStart(mainTxtTypeArea, e.getOffset());
- int end = Utilities.getWordEnd(mainTxtTypeArea, e.getOffset());
- int length = end - start;
- String word = mainTxtTypeArea.getDocument().getText(start, length);
- if(dictionary.find(word) == false && Character.isWhitespace(word.charAt(0)) == false){
- System.out.println("Incorrect: "+word);
- //I found a zigzag highlighter online, but the default yields the same result
- redZigZag.addHighlight(start, end, painter);
- } else {
- System.out.println("Correct: "+word);
- }
- } catch (BadLocationException ex) {}
- }
- private void mainTxtTypeAreaKeyReleased(java.awt.event.KeyEvent evt) {
- checkWord();
- }
- private void checkWord() {
- try {
- int start = Utilities.getWordStart(mainTxtTypeArea, mainTxtTypeArea.getCaretPosition());
- int end = Utilities.getWordEnd(mainTxtTypeArea, mainTxtTypeArea.getCaretPosition());
- int length = end - start;
- if(length > 0) {
- StyledDocument doc = mainTxtTypeArea.getStyledDocument();
- SimpleAttributeSet as = new SimpleAttributeSet();
- if(dictionary.find(doc.getText(start, length)) == false){
- StyleConstants.setItalic(as, true);
- StyleConstants.setForeground(as, Color.red);
- } else {
- StyleConstants.setItalic(as, false);
- StyleConstants.setForeground(as, Color.BLACK);
- }
- doc.setCharacterAttributes(start, length, as, false);
- }
- } catch (BadLocationException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- }