start Of Word of Swing Document - Java javax.swing.text

Java examples for javax.swing.text:StyledDocument

Description

start Of Word of 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 startOfWord(Document doc, int position) {
        try {/*from  w w w .  j a v  a  2s. c  om*/

            boolean found = false;
            while (!found) {
                if (position == 0) {
                    found = true;
                } else {
                    if (isLetter(doc.getText(position - 1, 1).charAt(0))) {
                        position--;
                    } else {
                        found = true;
                    }
                    ;

                }
            }
        } catch (BadLocationException ex) {
            DocUtils.writeMsg("BadLocationException in startOfWord "
                    + 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