Java Utililty Methods JTextComponent Word

List of utility methods to do JTextComponent Word

Description

The list of methods to do JTextComponent Word are organized into topic(s).

Method

intgetNextWord(JTextComponent c, int offs)
Determines the start of the next word for the given location.
int nextWord;
Element line = getParagraphElement(c, offs);
for (nextWord = getNextWordInParagraph(c, line, offs,
        false); nextWord == BreakIterator.DONE; nextWord = getNextWordInParagraph(c, line, offs, true)) {
    offs = line.getEndOffset();
    line = getParagraphElement(c, offs);
return nextWord;
...
intgetNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first)
Finds the next word in the given elements text.
if (line == null) {
    throw new BadLocationException("No more words", offs);
Document doc = line.getDocument();
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
if ((offs >= lineEnd) || (offs < lineStart)) {
    throw new BadLocationException("No more words", offs);
...
StringgetPartialWordBeforePos(JTextComponent editor, int iPos)
get Partial Word Before Pos
try {
    int iNonWhitespace = findNonWhitespacePositionBefore(editor.getText(), iPos - 1);
    int iStart = getWordStart(editor, iNonWhitespace);
    return editor.getText(iStart, iPos - iStart).trim();
} catch (BadLocationException e) {
    return "";
intgetPreviousWord(JTextComponent editor, int iOffset)
get Previous Word
String text = editor.getText();
int iStart = getWordStart(editor, iOffset);
for (iOffset = iStart - 1; iOffset >= 0 && Character.isWhitespace(text.charAt(iOffset)); iOffset--)
    ;
if (iOffset < 0) {
    return iStart;
return getWordStart(editor, iOffset);
...
StringgetWordAtCaret(JTextComponent editor)
get Word At Caret
try {
    int iCaretPos = editor.getCaretPosition();
    int iStart = getWordStart(editor, iCaretPos);
    int iEnd = getWordEnd(editor, iCaretPos);
    return editor.getText(iStart, iEnd - iStart);
} catch (BadLocationException e) {
    return getPartialWordBeforeCaret(editor);
DimensiongetWordDimensionAtCaret(JTextComponent editor)
get Word Dimension At Caret
try {
    int iCaretPos = editor.getCaretPosition();
    int iStart = getWordStart(editor, iCaretPos);
    int iEnd = getWordEnd(editor, iCaretPos);
    editor.getText(iStart, iEnd - iStart);
    return new Dimension(iStart, iEnd);
} catch (BadLocationException e) {
    return getWordDimensionBeforeCaret(editor);
...
DimensiongetWordDimensionBeforeCaret(JTextComponent editor)
get Word Dimension Before Caret
try {
    int iEnd = editor.getCaretPosition();
    int iStart = getPreviousWord(editor, iEnd);
    editor.getText(iStart, iEnd - iStart);
    return new Dimension(iStart, iEnd);
} catch (BadLocationException e) {
    return null;
intgetWordEnd(JTextComponent c, int offs)
Determines the end of a word for the given model location.
int result = Utilities.getWordEnd(c, offs);
if (result > 0) {
    char ch = c.getDocument().getText(result - 1, 1).charAt(0);
    if (isDirectionChar(ch)) {
        result--;
return result;
...
intgetWordEnd(JTextComponent editor, int iOffset)
get Word End
String text = editor.getText();
iOffset = maybeAdjustOffsetToNextWord(text, iOffset);
int iEnd = iOffset;
for (; iEnd < text.length() && Character.isJavaIdentifierPart(text.charAt(iEnd)); iEnd++)
    ;
if (iEnd == iOffset && !Character.isWhitespace(text.charAt(iEnd))) {
    iEnd++;
return iEnd;
intgetWordStart(JTextComponent c, int offs)
Determines the start of a word for the given model location.
Document doc = c.getDocument();
Element line = getParagraphElement(c, offs);
if (line == null) {
    throw new BadLocationException("No word at " + offs, offs);
int lineStart = line.getStartOffset();
int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
String s = doc.getText(lineStart, lineEnd - lineStart);
...