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:Main.java

/**
 * When the text is longer than the text box it gets centered, this
 * method ensures they all show the end of the text, eg the filename.
 *///  w w  w . j  ava  2s.  co m
public static void setCaretPosition(JComboBox comboBox) {
    Object item = comboBox.getSelectedItem();
    if (item != null) {
        String val = item.toString();
        JTextComponent c = (JTextComponent) comboBox.getEditor().getEditorComponent();
        c.setCaretPosition(val.length());
    }
}

From source file:Main.java

/**
 * Pastes via the fake clipboard./*  ww  w.  ja  v  a2s . co  m*/
 */

static void fakePaste(JTextComponent tc) {
    String text = tc.getText();
    int caretPos = tc.getCaretPosition();

    tc.setText(text.substring(0, caretPos) + clipboard + text.substring(caretPos));
    tc.setCaretPosition(caretPos + clipboard.length());
}

From source file:Main.java

public static void refreshJTextComponent(JTextComponent textComponent) {
    // borrowed from metaphase editor
    int pos = textComponent.getCaretPosition();
    textComponent.setText(textComponent.getText());
    textComponent.validate();/* w  w w  .j a  va2s. c om*/
    try {
        textComponent.setCaretPosition(pos);
    } catch (IllegalArgumentException e) {
        // swallow the exception
        // seems like a bug in the JTextPane component
        // only happens occasionally when pasting text at the end of a document
        System.err.println(e.getMessage());
    }
}

From source file:Main.java

/**
 * Cuts via the fake clipboard.// w ww  .  j  a v a 2s  . c  o m
 */

static void fakeCut(JTextComponent tc) {
    String selection = tc.getSelectedText();
    if ((selection != null) && (selection.length() > 0)) {
        clipboard = selection;

        String text = tc.getText();
        int selectionStart = tc.getSelectionStart();
        int selectionEnd = tc.getSelectionEnd();

        tc.setText(text.substring(0, selectionStart) + text.substring(selectionEnd));
        tc.setCaretPosition(selectionStart);
    }
}

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  w  ww  .  j av a  2 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:Main.java

public NavigationFilterPrefixWithBackspace(int prefixLength, JTextComponent component) {
    this.prefixLength = prefixLength;
    deletePrevious = component.getActionMap().get("delete-previous");
    component.getActionMap().put("delete-previous", new BackspaceAction());
    component.setCaretPosition(prefixLength);
}

From source file:com.mindcognition.mindraider.ui.swing.concept.annotation.renderer.AbstractTextAnnotationRenderer.java

/**
 * Search annotation from the current carret position.
 */// w  ww .  ja  v a  2s  . c  o  m
public void searchAnnotation(String searchString, boolean again) {
    logger.debug("searchAnnotation() " + searchString); // {{debug}}
    JTextComponent textComponent;
    if (inViewMode()) {
        textComponent = viewer;
        logger.debug("Searching viewer..."); // {{debug}}
    } else {
        textComponent = editor;
        logger.debug("Searching editor..."); // {{debug}}
    }

    if (!again) {
        textComponent.setCaretPosition(0);
    }

    if (searchString != null && searchString.length() > 0) {
        Document document = textComponent.getDocument();
        try {
            int idx = textComponent.getDocument().getText(0, document.getLength()).indexOf(searchString,
                    textComponent.getCaretPosition());
            if (idx < 0) {
                // try it from the beginning
                idx = textComponent.getDocument().getText(0, document.getLength()).indexOf(searchString);
            } else if (idx > 0) {
                textComponent.setCaretPosition(idx);
                textComponent.requestFocus();
                textComponent.select(idx, idx + searchString.length());
            }
        } catch (Exception e) {
            // TODO no bundle!
            logger.debug(Messages.getString("ConceptJPanel.unableToSearch", e.getMessage()));
        }
    }
}

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

public void clearCurrentSuggestion(JTextComponent comp) {
    if (toSetIn != null) {
        int selStart = comp.getSelectionStart();
        String text = comp.getText();
        comp.setText(text.substring(0, selStart) + text.substring(comp.getSelectionEnd()));
        comp.setCaretPosition(selStart);
        lastCompletions = null;/*from ww w  . j  a  v  a  2 s  .  c om*/
        lastShownCompletion = 0;
        lastCaretPosition = -1;
        toSetIn = null;
    }
}

From source file:net.sf.jabref.gui.keyboard.EmacsKeyBindings.java

private static void doCopyOrCut(JTextComponent jtc, boolean copy) {
    if (jtc != null) {
        int caretPosition = jtc.getCaretPosition();
        String text = jtc.getSelectedText();
        if (text != null) {
            // user has manually marked a text without using CTRL+W
            // we obey that selection and copy it.
        } else if (SetMarkCommandAction.isMarked(jtc)) {
            int beginPos = caretPosition;
            int endPos = SetMarkCommandAction.getCaretPosition();
            if (beginPos > endPos) {
                int tmp = endPos;
                endPos = beginPos;/* w ww  . j a  v  a 2 s  .c  o  m*/
                beginPos = tmp;
            }
            jtc.select(beginPos, endPos);
            SetMarkCommandAction.reset();
        }
        text = jtc.getSelectedText();
        if (text == null) {
            jtc.getToolkit().beep();
        } else {
            if (copy) {
                jtc.copy();
                // clear the selection
                jtc.select(caretPosition, caretPosition);
            } else {
                int newCaretPos = jtc.getSelectionStart();
                jtc.cut();
                // put the cursor to the beginning of the text to cut
                jtc.setCaretPosition(newCaretPos);
            }
            KillRing.getInstance().add(text);
        }
    }
}

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

/** Select or grow image when clicked.
  *///from w  ww.j a  va  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);
            }
        }
    }
}