Get the end Of Word in Swing Document - Java javax.swing.text

Java examples for javax.swing.text:StyledDocument

Description

Get the end Of Word in Swing Document

Demo Code


import javax.swing.text.*;
import java.util.*;
import javax.swing.JTextPane;
import java.awt.Color;

public class Main{
    public static int endOfWord(Document doc, int position) {
        try {//from   w  w w.j a v a2 s. c o m
            while (position < doc.getLength()
                    && isLetter(doc.getText(position, 1).charAt(0))) {
                position++;
            }
            ;
        } catch (BadLocationException ex) {
            DocUtils.writeMsg("BadLocationException in endOfWord "
                    + position);
            ex.printStackTrace();
            System.exit(1);
        }
        ;

        return position;
    }
    public static boolean isLetter(char c) {
        return (Character.isAlphabetic(c) || (c == '\''));
    }
    public static void writeMsg(String msg) {
        JTextPane pane;
        Document doc;
        pane = TurkEditor.turkEditor.msgArea;
        doc = pane.getDocument();
        try {
            doc.insertString(doc.getLength(), msg + "\n", null);
        } catch (BadLocationException blex) {
        }

        pane.setCaretPosition(doc.getLength());

    }
}

Related Tutorials