List of utility methods to do JTextComponent Caret
int | getCaretCol(int idx, JTextComponent comp) Method getCaretCol. return Math.abs(Utilities.getRowStart(comp, idx) - idx);
|
int | getCaretRow(int idx, JTextComponent comp) Method getCaretRow. return getLineOfOffset(idx, comp.getDocument());
|
int | getLineAtCaret(final JTextComponent component) get Line At Caret final int caretPosition = component.getCaretPosition(); final Element root = component.getDocument().getDefaultRootElement(); return root.getElementIndex(caretPosition) + 1; |
FocusListener | makeCaretAlwaysVisible(final JTextComponent comp) Make caret visible even when the JTextComponent is not editable. FocusListener listener = new FocusAdapter() { @Override public void focusGained(FocusEvent e) { Caret caret = comp.getCaret(); caret.setVisible(true); caret.setSelectionVisible(true); }; ... |
void | selectLineAtCaret(JTextComponent editor) select Line At Caret int lineStart = getLineStart(editor.getText(), editor.getCaretPosition()); int lineEnd = getLineEnd(editor.getText(), editor.getCaretPosition()); if (lineEnd < editor.getText().length()) { lineEnd++; editor.getCaret().setDot(lineEnd); editor.getCaret().moveDot(lineStart); |
void | setCaretUpdateEnabled(JTextComponent comp, boolean updateEnabled) set Caret Update Enabled Caret caret = comp.getCaret(); if (caret instanceof DefaultCaret) { ((DefaultCaret) caret) .setUpdatePolicy(updateEnabled ? DefaultCaret.UPDATE_WHEN_ON_EDT : DefaultCaret.NEVER_UPDATE); |
void | setTextAndResetCaret(JTextComponent component, String text) set Text And Reset Caret component.setText(text); component.setCaretPosition(0); |