Example usage for javax.swing.text JTextComponent setCaretPosition

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

Introduction

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

Prototype

@BeanProperty(bound = false, description = "the caret position")
public void setCaretPosition(int position) 

Source Link

Document

Sets the position of the text insertion caret for the TextComponent.

Usage

From source file:org.executequery.gui.text.TextUtilities.java

public static void insertLineAfter(JTextComponent textComponent) {
    String newLine = "\n";
    int caretIndex = textComponent.getCaretPosition();

    StringBuilder sb = new StringBuilder(textComponent.getText());

    int endOfLineIndex = sb.indexOf(newLine, caretIndex);

    int length = sb.length();

    if (caretIndex == length || endOfLineIndex == length)
        sb.append(newLine);//from   w  w w . j a v  a  2 s  . c om
    else
        sb.insert(endOfLineIndex == -1 ? 0 : endOfLineIndex, newLine);

    textComponent.setText(sb.toString());
    textComponent.setCaretPosition(endOfLineIndex == -1 ? length : endOfLineIndex + 1);
    sb = null;
}

From source file:org.executequery.gui.text.TextUtilities.java

public static void insertLineBefore(JTextComponent textComponent) {
    int caretIndex = textComponent.getCaretPosition();
    int insertIndex = -1;
    char newLine = '\n';

    String text = textComponent.getText();
    char[] textChars = text.toCharArray();

    for (int i = 0; i < textChars.length; i++) {

        if (i > caretIndex) {
            break;
        }/*from   ww  w  .j ava 2  s.  c o  m*/

        else {

            if (textChars[i] == newLine)
                insertIndex = i;

        }

    }

    StringBuilder sb = new StringBuilder(text);
    sb.insert(insertIndex == -1 ? 0 : insertIndex, newLine);

    textComponent.setText(sb.toString());
    textComponent.setCaretPosition(insertIndex + 1);
}

From source file:org.parosproxy.paros.view.FindDialog.java

private void find() {
    JTextComponent txtComp = lastInvoker;
    if (txtComp == null) {
        JFrame parent = (JFrame) (this.getParent());
        Component c = parent.getMostRecentFocusOwner();
        if (c instanceof JTextComponent) {
            txtComp = (JTextComponent) c;
        }// w w w  . j a va 2  s. c o m
    }

    // ZAP: Check if a JTextComponent was really found.
    if (txtComp == null) {
        return;
    }

    try {
        String findText = txtFind.getText().toLowerCase();
        String txt = txtComp.getText().toLowerCase();
        int startPos = txt.indexOf(findText, txtComp.getCaretPosition());

        // Enable Wrap Search
        if (startPos <= 0) {
            txtComp.setCaretPosition(0);
            startPos = txt.indexOf(findText, txtComp.getCaretPosition());
        }

        int length = findText.length();
        if (startPos > -1) {
            txtComp.select(startPos, startPos + length);
            txtComp.requestFocusInWindow();
            txtFind.requestFocusInWindow();
        } else {
            Toolkit.getDefaultToolkit().beep();
        }
    } catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());
    }
}

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

/**
 * Select or grow image when clicked./*from www .j av a  2 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);
            }
        }
    }
}