boolean (int caretOffset, Document document) { for (int i = caretOffset; i < document.getTextLength(); i++) { char c = document.getCharsSequence().charAt(i); if (c == '\n') return false; if (!Character.isWhitespace(c)) return true; } return false; // insert at the end of the document }