List of usage examples for javax.swing.text JTextComponent getCaret
@Transient
public Caret getCaret()
From source file:Main.java
public static void main(String[] argv) { JTextComponent c = new JTextArea(); // Set rate to blink once a second c.getCaret().setBlinkRate(1000); // Set the caret to stop blinking c.getCaret().setBlinkRate(0);/* ww w .j a v a2s . co m*/ }
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 *//*w w w.j ava 2 s . c om*/ 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 void highLight(JTextComponent component, String patteren) { try {/*from w w w .java 2 s. co 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(); } }
From source file:FormattedTextFieldExample.java
protected void drawLine(int line, Graphics g, int x, int y) { // Set the colors JTextComponent host = (JTextComponent) getContainer(); unselected = (host.isEnabled()) ? host.getForeground() : host.getDisabledTextColor(); Caret c = host.getCaret(); selected = c.isSelectionVisible() ? host.getSelectedTextColor() : unselected; int p0 = element.getStartOffset(); int p1 = element.getEndOffset() - 1; int sel0 = ((JTextComponent) getContainer()).getSelectionStart(); int sel1 = ((JTextComponent) getContainer()).getSelectionEnd(); try {//from w w w .j ava2 s. c om // If the element is empty or there is no selection // in this view, just draw the whole thing in one go. if (p0 == p1 || sel0 == sel1 || inView(p0, p1, sel0, sel1) == false) { drawUnselectedText(g, x, y, 0, contentBuff.count); return; } // There is a selection in this view. Draw up to three regions: // (a) The unselected region before the selection. // (b) The selected region. // (c) The unselected region after the selection. // First, map the selected region offsets to be relative // to the start of the region and then map them to view // offsets so that they take into account characters not // present in the model. int mappedSel0 = mapOffset(Math.max(sel0 - p0, 0)); int mappedSel1 = mapOffset(Math.min(sel1 - p0, p1 - p0)); if (mappedSel0 > 0) { // Draw an initial unselected region x = drawUnselectedText(g, x, y, 0, mappedSel0); } x = drawSelectedText(g, x, y, mappedSel0, mappedSel1); if (mappedSel1 < contentBuff.count) { drawUnselectedText(g, x, y, mappedSel1, contentBuff.count); } } catch (BadLocationException e) { // Should not happen! } }
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.executequery.gui.editor.autocomplete.QueryEditorAutoCompletePopupProvider.java
public void firePopupTrigger() { try {/*www . ja va 2s . c o m*/ final JTextComponent textComponent = queryEditorTextComponent(); Caret caret = textComponent.getCaret(); final Rectangle caretCoords = textComponent.modelToView(caret.getDot()); addFocusActions(); resetCount = 0; captureAndResetListValues(); popupMenu().show(textComponent, caretCoords.x, caretCoords.y + caretCoords.height); textComponent.requestFocus(); } catch (BadLocationException e) { debug("Error on caret coordinates", e); } }
From source file:org.pmedv.core.components.RelativeImageView.java
/** * Select or grow image when clicked.//from w w w. j a v a 2s.c om * * @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); } } } }