Example usage for javax.swing.text JTextComponent getText

List of usage examples for javax.swing.text JTextComponent getText

Introduction

In this page you can find the example usage for javax.swing.text JTextComponent getText.

Prototype

public String getText(int offs, int len) throws BadLocationException 

Source Link

Document

Fetches a portion of the text represented by the component.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent c = new JTextArea();
    c.getCaretPosition();//from   ww  w.  ja  v  a  2  s . com
    if (c.getCaretPosition() < c.getDocument().getLength()) {
        char ch = c.getText(c.getCaretPosition(), 1).charAt(0);
    }
    // Set the caret color
    c.setCaretColor(Color.red);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent c = new JTextArea();
    c.getCaretPosition();/*from  w ww.j  a  v  a  2 s .  c o m*/
    if (c.getCaretPosition() < c.getDocument().getLength()) {
        char ch = c.getText(c.getCaretPosition(), 1).charAt(0);
    }
    // Move the caret
    int newPosition = 0;
    c.moveCaretPosition(newPosition);
}

From source file:Main.java

public void highLight(JTextComponent component, String patteren) {
    try {// www .  jav  a 2  s  .c  om
        Document doc = component.getDocument();
        String text = component.getText(0, doc.getLength());
        int pos = component.getCaretPosition();
        if (pos == doc.getLength()) {
            pos = 0;
        }
        int index = text.toUpperCase().indexOf(patteren.toUpperCase(), pos);
        if (index >= 0) {
            component.setSelectionStart(index);
            component.setSelectionEnd(index + patteren.length());
            component.getCaret().setSelectionVisible(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:FancyCaret.java

public void paint(Graphics g) {
    JTextComponent comp = getComponent();
    if (comp == null)
        return;/*from ww w  .j  ava 2  s .  c  o m*/

    int dot = getDot();
    Rectangle r = null;
    char dotChar;
    try {
        r = comp.modelToView(dot);
        if (r == null)
            return;
        dotChar = comp.getText(dot, 1).charAt(0);
    } catch (BadLocationException e) {
        return;
    }

    if ((x != r.x) || (y != r.y)) {
        repaint();
        x = r.x;
        y = r.y;
        height = r.height;
    }

    g.setColor(comp.getCaretColor());
    g.setXORMode(comp.getBackground());

    if (dotChar == '\n') {
        int diam = r.height;
        if (isVisible())
            g.fillArc(r.x - diam / 2, r.y, diam, diam, 270, 180); // half circle
        width = diam / 2 + 2;
        return;
    }

    if (dotChar == '\t')
        try {
            Rectangle nextr = comp.modelToView(dot + 1);
            if ((r.y == nextr.y) && (r.x < nextr.x)) {
                width = nextr.x - r.x;
                if (isVisible())
                    g.fillRoundRect(r.x, r.y, width, r.height, 12, 12);
                return;
            } else
                dotChar = ' ';
        } catch (BadLocationException e) {
            dotChar = ' ';
        }

    width = g.getFontMetrics().charWidth(dotChar);
    if (isVisible())
        g.fillRect(r.x, r.y, width, r.height);
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

protected int findNamePositionStatus(JTextComponent comp) {
    String upToCaret;/*from   w w  w. j av  a  2  s  .  c  o  m*/
    try {
        upToCaret = comp.getText(0, comp.getCaretPosition());
        // Clip off evertyhing up to and including the last " and " before:
        upToCaret = upToCaret.substring(upToCaret.lastIndexOf(" and ") + 1);
        int commaIndex = upToCaret.indexOf(',');
        if (commaIndex < 0) {
            return AutoCompleteListener.ANY_NAME;
        } else {
            return AutoCompleteListener.FIRST_NAME;
        }

    } catch (BadLocationException ex) {
        return AutoCompleteListener.ANY_NAME;
    }

}

From source file:net.sf.jabref.gui.autocompleter.AutoCompleteListener.java

private StringBuffer getCurrentWord(JTextComponent comp) {
    StringBuffer res = new StringBuffer();
    String upToCaret;/*from   w  ww  .  j  ava 2 s  .co  m*/

    try {
        upToCaret = comp.getText(0, comp.getCaretPosition());
        // We now have the text from the start of the field up to the caret position.
        // In most fields, we are only interested in the currently edited word, so we
        // seek from the caret backward to the closest space:
        if (!completer.isSingleUnitField()) {
            if ((comp.getCaretPosition() < comp.getText().length())
                    && Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
                // caret is in the middle of the text AND current character is a whitespace
                // that means: a new word is started and there is no current word
                return new StringBuffer();
            }

            int piv = upToCaret.length() - 1;
            while ((piv >= 0) && !Character.isWhitespace(upToCaret.charAt(piv))) {
                piv--;
            }
            // piv points to whitespace char or piv is -1
            // copy everything from the next char up to the end of "upToCaret"
            res.append(upToCaret.substring(piv + 1));
        } else {
            // For fields such as "journal" it is more reasonable to try to complete on the entire
            // text field content, so we skip the searching and keep the entire part up to the caret:
            res.append(upToCaret);
        }
        LOGGER.debug("AutoCompListener: " + res);
    } catch (BadLocationException ignore) {
        // Ignored
    }

    return res;
}

From source file:net.sf.jabref.gui.AutoCompleteListener.java

private StringBuffer getCurrentWord(JTextComponent comp) {
    StringBuffer res = new StringBuffer();
    String upToCaret;//  w w  w  . j  ava 2 s  . c  om

    try {
        upToCaret = comp.getText(0, comp.getCaretPosition());
        // We now have the text from the start of the field up to the caret position.
        // In most fields, we are only interested in the currently edited word, so we
        // seek from the caret backward to the closest space:
        if (!completer.isSingleUnitField()) {
            if ((comp.getCaretPosition() < comp.getText().length())
                    && Character.isWhitespace(comp.getText().charAt(comp.getCaretPosition()))) {
                // caret is in the middle of the text AND current character is a whitespace
                // that means: a new word is started and there is no current word
                return null;
            }

            int piv = upToCaret.length() - 1;
            while ((piv >= 0) && !Character.isWhitespace(upToCaret.charAt(piv))) {
                piv--;
            }
            // priv points to whitespace char or priv is -1
            // copy everything from the next char up to the end of "upToCaret" 
            res.append(upToCaret.substring(piv + 1));
        } else {
            // For fields such as "journal" it is more reasonable to try to complete on the entire
            // text field content, so we skip the searching and keep the entire part up to the caret:
            res.append(upToCaret);
        }
        //Util.pr("AutoCompListener: "+res.toString());
    } catch (BadLocationException ignore) {
    }

    return res;
}