document.insertString(currentWordPos, newWord, attr);
document.insertString(currentWordPos, newWord, null);
  public void replaceWord(String newWord) {    AttributeSet attr=null;    if (currentWordPos != -1) {      try {        if(document instanceof StyledDocument)            attr=((StyledDocument)document).getCharacterElement(currentWordPos).getAttributes();        document.remove(currentWordPos, currentWordEnd - currentWordPos);        document.insertString(currentWordPos, newWord, attr);        //Need to reset the segment        document.getText(0, document.getLength(), text);      } catch (BadLocationException ex) {        throw new RuntimeException(ex.getMessage());      }      //Position after the newly replaced word(s)      first = true;      currentWordPos = getNextWordStart(text, currentWordPos + newWord.length());      if (currentWordPos != -1) {        currentWordEnd = getNextWordEnd(text, currentWordPos);        nextWordPos = getNextWordStart(text, currentWordEnd);        sentenceIterator.setText(text);        sentenceIterator.following(currentWordPos);      } else        moreTokens = false;    }  }