Example usage for javax.swing.text JTextComponent moveCaretPosition

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

Introduction

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

Prototype

public void moveCaretPosition(int pos) 

Source Link

Document

Moves the caret to a new position, leaving behind a mark defined by the last time setCaretPosition was called.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JTextComponent c = new JTextArea();
    c.getCaretPosition();//from  w  w w .j av a  2s  .  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

/**
 * sets text of textComp without moving its caret.
 *
 * @param textComp  text component whose text needs to be set
 * @param text      text to be set. null will be treated as empty string
 *//*from   ww  w . ja  v  a2  s  .  c  o  m*/
public static void setText(JTextComponent textComp, String text) {
    if (text == null)
        text = "";

    if (textComp.getCaret() instanceof DefaultCaret) {
        DefaultCaret caret = (DefaultCaret) textComp.getCaret();
        int updatePolicy = caret.getUpdatePolicy();
        caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
        try {
            textComp.setText(text);
        } finally {
            caret.setUpdatePolicy(updatePolicy);
        }
    } else {
        int mark = textComp.getCaret().getMark();
        int dot = textComp.getCaretPosition();
        try {
            textComp.setText(text);
        } finally {
            int len = textComp.getDocument().getLength();
            if (mark > len)
                mark = len;
            if (dot > len)
                dot = len;
            textComp.setCaretPosition(mark);
            if (dot != mark)
                textComp.moveCaretPosition(dot);
        }
    }
}

From source file:com.hexidec.ekit.component.RelativeImageView.java

/** Select or grow image when clicked.
  *//*www. j  a v  a  2 s. c  o  m*/
public void mousePressed(MouseEvent e) {
    Dimension size = fComponent.getSize();
    if ((e.getX() >= (size.width - 7)) && (e.getY() >= (size.height - 7)) && (getSelectionState() == 2)) {
        // Click in selected grow-box:
        Point loc = fComponent.getLocationOnScreen();
        fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight);
        fGrowProportionally = e.isShiftDown();
    } else {
        // Else select image:
        fGrowBase = null;
        JTextComponent comp = (JTextComponent) fContainer;
        int start = fElement.getStartOffset();
        int end = fElement.getEndOffset();
        int mark = comp.getCaret().getMark();
        int dot = comp.getCaret().getDot();
        if (e.isShiftDown()) {
            // extend selection if shift key down:
            if (mark <= start) {
                comp.moveCaretPosition(end);
            } else {
                comp.moveCaretPosition(start);
            }
        } else {
            // just select image, without shift:
            if (mark != start) {
                comp.setCaretPosition(start);
            }
            if (dot != end) {
                comp.moveCaretPosition(end);
            }
        }
    }
}

From source file:org.pmedv.core.components.RelativeImageView.java

/**
 * Select or grow image when clicked.//  www.j  a  v a2 s.  c o  m
 * 
 * @param e
 *            MouseEvent to handle
 */
public void mousePressed(MouseEvent e) {

    Dimension size = fComponent.getSize();
    if ((e.getX() >= (size.width - 7)) && (e.getY() >= (size.height - 7)) && (getSelectionState() == 2)) {
        Point loc = fComponent.getLocationOnScreen();
        fGrowBase = new Point(loc.x + e.getX() - fWidth, loc.y + e.getY() - fHeight);
        fGrowProportionally = e.isShiftDown();
    } else {
        fGrowBase = null;
        JTextComponent comp = (JTextComponent) fContainer;
        int start = fElement.getStartOffset();
        int end = fElement.getEndOffset();
        int mark = comp.getCaret().getMark();
        int dot = comp.getCaret().getDot();
        if (e.isShiftDown()) {

            if (mark <= start) {
                comp.moveCaretPosition(end);
            } else {
                comp.moveCaretPosition(start);
            }
        } else {
            if (mark != start) {
                comp.setCaretPosition(start);
            }
            if (dot != end) {
                comp.moveCaretPosition(end);
            }
        }
    }
}