List of usage examples for javax.swing.text JTextComponent getCaretPosition
@Transient public int getCaretPosition()
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent c = new JTextArea(); c.getCaretPosition(); if (c.getCaretPosition() < c.getDocument().getLength()) { char ch = c.getText(c.getCaretPosition(), 1).charAt(0); }/*from w w w . ja v a 2s . c o m*/ // 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(); if (c.getCaretPosition() < c.getDocument().getLength()) { char ch = c.getText(c.getCaretPosition(), 1).charAt(0); }// ww w . ja v a 2 s . c o m // Move the caret int newPosition = 0; c.moveCaretPosition(newPosition); }
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();//from ww w . j a va 2s. 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
/** * Pastes via the fake clipboard./* www. j av a2 s .c o 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
/** * 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 w w .ja v 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: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 w w . j av a 2 s .co 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:InsertAction.java
public void actionPerformed(ActionEvent evt) { JTextComponent c = (JTextComponent) evt.getSource(); try {//from w ww . j a va 2 s . c o m c.getDocument().insertString(c.getCaretPosition(), " space", null); } catch (BadLocationException e) { } }
From source file:Main.java
public void actionPerformed(ActionEvent evt) { JTextComponent c = (JTextComponent) evt.getSource(); try {//from w w w. j av a 2 s . com c.getDocument().insertString(c.getCaretPosition(), " ", null); } catch (Exception e) { e.printStackTrace(); } }
From source file:MyKeyListener.java
public void keyTyped(KeyEvent evt) { JTextComponent c = (JTextComponent) evt.getSource(); char ch = evt.getKeyChar(); if (Character.isLowerCase(ch) == false) { return;/*from w w w . j a v a 2 s.co m*/ } try { c.getDocument().insertString(c.getCaretPosition(), "" + Character.toUpperCase(ch), null); evt.consume(); } catch (BadLocationException e) { } }
From source file:Main.java
public void highLight(JTextComponent component, String patteren) { try {/*www. jav a2 s.c o m*/ 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(); } }